Fixing the pandas error when attempting to append DataFrames with version 2.0
Now that a more moderen major pandas version has been released, among the previously deprecated functionality in older versions will not work. One such methods is pandas.DataFrame.append()
that has been deprecated since pandas version 1.4.0 (the identical applies for the Series API, pandas.Series.append()
).
As of pandas version 2.0.0, the append()
method has been compltely removed (see issue #35407). Due to this fact, code referncing this method will now report the next error:
AttributeError: 'DataFrame' object has no attribute 'append'
On this short tutorial, we’ll show how a pandas DataFrame will be concatenated with other DataFrame (and even other Python objects, resembling dictionaries), using the brand new, really useful syntax.
Find out how to concatenate pandas DataFrames in pandas v2.0.0+
In older panads versins, the concatenation of DataFrames might have been achieved with the usage of appned()
method. The instance code snippet below demonstrates how pandas versions older than v2.0 might be used to accomplish that:
# Old syntax
import pandas as pd# DataFrames
df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
df1.append(df2)
# Series
pd.Series([1, 2]).append(pd.Series([3, 4])
As of pandas 2.0, with a purpose to concatenate multiple DataFrames or Series together you need to as a substitute call the concat()
method, as illustrated below:
# Latest syntax, compatible with pandas v2.0
import pandas as pd# DataFrames
df1 = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
pd.concat([df1, df2])
# Series
pd.concat([pd.Series([1, 2]), pd.Series([3, 4])])
What pandas version am I running?
Should you are unsure about what pandas version you’re running, there are a number of other ways to seek out out. You possibly can programmatically accomplish that by calling the next snippet:
import pandas as pd print(pd.__version__)
Alternatively, yow will discover out directly from pip by running the next command (be sure that you’re in the specified virtual environment, if you happen to use one):
$ pip list | grep pandas
Final Thoughts
On this short tutorial, we discussed how you may potentially concatenate Pandas DataFrames using the brand new, really useful syntax following the brand new major release of pandas 2.0.
Now that append()
method (deprecated as of v1.4.0) has been completely removed as of pandas 2.0, the brand new concat()
method must be used as a substitute. The identical applies for the pandas Series API that not has the append
member.