Last updated: Apr 12, 2024
Reading time·4 min
The Pandas TypeError "Cannot concatenate object of type 'X'; only Series and
DataFrame objs are valid" occurs when you pass an invalid first argument to
the pandas.concat()
method.
To solve the error, make sure to pass a list of DataFrame
or Series
to the
pandas.concat()
method.
Here is an example of how the error occurs.
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) dataframes = [df1, df2] # ⛔️ TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid df3 = pd.concat([dataframes], ignore_index=True) print(df3)
We have a dataframes
variable that stores 2 DataFrame
objects.
However, notice that we passed a list containing the variable to the pandas.concat method.
concat
method.To solve the issue in the example, pass a one-dimensional list of DataFrames to
pandas.concat()
.
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) dataframes = [df1, df2] df3 = pd.concat(dataframes, ignore_index=True) # A B # 0 1 3 # 1 2 4 # 2 5 7 # 3 6 8 print(df3)
We could've also solved the error by passing an inline list containing the
DataFrames to pandas.concat()
.
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) df3 = pd.concat([df1, df2], ignore_index=True) # A B # 0 1 3 # 1 2 4 # 2 5 7 # 3 6 8 print(df3)
The pandas.concat()
method concatenates pandas objects along a particular
axis.
The first argument the method takes is a sequence or a mapping of DataFrame
objects or Series
.
You will also get the error if you pass a list containing dictionaries to the
pandas.concat()
method.
import pandas as pd dict1 = { 'A': [1, 2], 'B': [3, 4] } dict2 = { 'A': [5, 6], 'B': [7, 8] } # ⛔️ TypeError: cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid df3 = pd.concat([dict1, dict2], ignore_index=True)
To solve the error in this case, convert the dictionaries to DataFrame
objects
by using the
pd.DataFrame()
constructor.
import pandas as pd dict1 = { 'A': [1, 2], 'B': [3, 4] } dict2 = { 'A': [5, 6], 'B': [7, 8] } df3 = pd.concat( [pd.DataFrame(dict1), pd.DataFrame(dict2)], ignore_index=True ) # A B # 0 1 3 # 1 2 4 # 2 5 7 # 3 6 8 print(df3)
We could've also converted the dictionaries to DataFrame
objects directly.
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) df3 = pd.concat( [df1, df2], ignore_index=True ) # A B # 0 1 3 # 1 2 4 # 2 5 7 # 3 6 8 print(df3)
pandas.concat
with a list of stringsYou should also ensure that you aren't calling the pandas.concat()
method with
a list containing strings.
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) # ⛔️ TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid df3 = pd.concat( ["df1", "df2"], ignore_index=True )
The issue in the code sample is that the variable names "df1"
and "df2"
are
enclosed in quotes in the list we passed to pandas.concat
.
Remove the quotation marks to solve the error.
import pandas as pd df1 = pd.DataFrame({ 'A': [1, 2], 'B': [3, 4] }) df2 = pd.DataFrame({ 'A': [5, 6], 'B': [7, 8] }) df3 = pd.concat( [df1, df2], ignore_index=True ) # A B # 0 1 3 # 1 2 4 # 2 5 7 # 3 6 8 print(df3)
pandas.concat()
with Series
objectsYou can also call the pandas.concat()
method with Series
objects.
import pandas as pd s1 = pd.Series(['a', 'b']) s2 = pd.Series(['c', 'd']) s3 = pd.concat( [s1, s2], ignore_index=True ) # 0 a # 1 b # 2 c # 3 d # dtype: object print(s3)
If you want to construct a hierarchical index, pass the keys
argument.
import pandas as pd s1 = pd.Series(['a', 'b']) s2 = pd.Series(['c', 'd']) df3 = pd.concat( [s1, s2], keys=['s1', 's2'] ) # s1 0 a # 1 b # s2 0 c # 1 d # dtype: object print(df3)
When concatenating all Series
along the index (axis=0), a Series
object is
returned.
If the supplied list (the first argument of concat
) contains at least one
DataFrame
, a DataFrame
is returned.
You can learn more about the related topics by checking out the following tutorials: