Error: 'DataFrame' Object Has No Attribute 'append'
When attempting to add a dictionary to a DataFrame object, users may encounter the error message "AttributeError: 'DataFrame' object has no attribute 'append'." While DataFrame was previously equipped with the append method, it was removed in pandas version 2.0.
Solution
To append data to a DataFrame, use the concat method instead:
1 |
|
Alternative Solution
If the index is a RangeIndex, the loc attribute can also be used:
1 |
|
Why the Change?
The append method was removed because it was an inefficient operation, especially when repeated. Unlike list.append, which operates in-place, DataFrame's append created a new DataFrame. This made repeated insertion operations quadratic in time complexity.
Best Practices for Repeated Insertion
Instead of using append or concat repeatedly, collect the new items into a list and convert them to a DataFrame at the end of the loop. Then, concatenate the new DataFrame to the original.
1 2 3 4 5 6 7 8 9 10 |
|
The above is the detailed content of Why is `DataFrame.append` Deprecated and How Do I Add Rows to a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!