Most Python developers take time to solve the AttributeError. There are many errors on AttributeError : dataframe object has no attribute. Attributeerror: ‘dataframe’ object has no attribute ‘append’ is one of them. In this post, you will all the solutions to resolve this error.
Why Attributeerror: ‘dataframe’ object has no attribute ‘append’?
The AttributeError: ‘DataFrame’ object has no attribute ‘append’ error occurs when you try to use the append
method on a Pandas DataFrame object. It is because Pandas DataFrames do not have a append
method by default to append the rows of the dataframe.
How to Solve the Attributeerror: ‘dataframe’ object has no attribute ‘append’ Error?
There are different ways to solve this error. Some of them are below.
Solution 1: Concatenate DataFrames:
Instead of using the append
method, you can concatenate multiple DataFrames using the concat()
function from Pandas. This way you will not get the AttributeError.
df1 = pd.DataFrame({'A': [10, 20], 'B': [30, 40]})
df2 = pd.DataFrame({'A': [50, 60], 'B': [70, 80]})
result = pd.concat([df1, df2])
Solution 2: Append rows using a dictionary or list
You can use the append()
function to add rows to a DataFrame by passing a dictionary or a list of dictionaries. Just pass the new row you want to add as an argument with the ignore_index=True
df = pd.DataFrame({'A': [10, 20], 'B': [30, 40]})
new_row = {'A': 50, 'B': 60}
df = df.append(new_row, ignore_index=True)
Solution 3: Append rows using Series
You can also use the append()
function to add rows to a DataFrame by passing a Series object. Below is an example of it.
df = pd.DataFrame({'A': [10, 20], 'B': [30, 40]})
new_row = pd.Series({'A': 50, 'B': 60})
df = df.append(new_row, ignore_index=True)
Conclusion
In most cases the error AttributeError comes when you are using the function that cannot be applied on the object. If you are getting Attributeerror: ‘dataframe’ object has no attribute ‘append’ then the above are some of the methods to solve the error. Choose the method that best fits your specific use case.
Leave a Reply