AttributeError: ‘DataFrame’ object has no attribute ‘concat’ error occurs when you are using the concat() method on a dataframe, and the dataframe object does not have a concat attribute.
To fix the AttributeError: ‘DataFrame’ object has no attribute ‘concat’ error, use the
Pandas concat()
method properly and don’t use the
concat()
method on
DataFrame.
Reproduce the error
import pandas as pd# Creating a simple dataframedf1 = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6]# Creating another dataframe to append to df1df2 = pd.DataFrame({ 'A': [7, 8, 9], 'B': [10, 11, 12]# concatenating df1 and df2df = df1.concat(df2)print(df)
Output
AttributeError: 'DataFrame' object has no attribute 'concat'
How to Fix It?
Use the pandas.concat() method and pass the df1, df2 as argument to concat both dataframes.
import pandas as pd# Creating a simple dataframedf1 = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6]# Creating another dataframe to append to df1df2 = pd.DataFrame({ 'A': [7, 8, 9], 'B': [10, 11, 12]# concatenating df1 and df2df = pd.concat([df1, df2])print(df)
Output
A B
0 1 4
1 2 5
2 3 6
0 7 10
1 8 11
2 9 12
And we fixed the error by implementing the solution! That’s all.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.