Reputation: 93
Hi I'm still learning pandas and numpy in python
I learn from e-learning that you could combine 2 series with append but when I tried it It give me error
students_classes = pd.Series({'Alice': 'Physics',
'Jack': 'Chemistry',
'Molly': 'English',
'Sam': 'History'})
students_classes
kelly_classes = pd.Series(['Philosophy', 'Arts', 'Math'], index=['Kelly', 'Kelly', 'Kelly'])
kelly_classes
all_students_classes = students_classes.append(kelly_classes)
all_students_classes
and It give me error like this
AttributeError Traceback (most recent call last)
Cell In\[35\], line 3
1 # Finally, we can append all of the data in this new Series to the first using the .append()
2 # function.
\----\> 3 all_students_classes.str = students_classes.append(kelly_classes)
5 # This creates a series which has our original people in it as well as all of Kelly's courses
6 all_students_classes
File \~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\pandas\\core\\generic.py:5989, in NDFrame.__getattr__(self, name)
5982 if (
5983 name not in self.\_internal_names_set
5984 and name not in self.\_metadata
5985 and name not in self.\_accessors
5986 and self.\_info_axis.\_can_hold_identifiers_and_holds_name(name)
5987 ):
5988 return self\[name\]
\-\> 5989 return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'append'
I expect something like this from the e-learning
Upvotes: 9
Views: 42243
Reputation: 1
yes,.append() has been removed.But we can still use ._append()
Upvotes: 0
Reputation: 29
When the append method cannot be replaced due to configured environment restrictions, I recommend you to use _append
Upvotes: 1
Reputation: 81
Probably an update of pandas may have changed the syntax, but you probably are looking for:
all_students_classes = students_classes._append(kelly_classes)
(quick tip, you can check the class definition in VS code by right cliking on the pd.Series and choosing Go to Definition, then you can see some of the methods defined at least). Otherwise use the concat
method that uses this.
Upvotes: 4
Reputation: 231615
In relatively recent pandas version, 1.5.2, append
works, but gives a warning.
In pd 2.0, append
has been removed
https://pandas.pydata.org/docs/dev/whatsnew/v2.0.0.html#deprecations
In [14]: students_classes = pd.Series({'Alice': 'Physics',
...: 'Jack': 'Chemistry',
...: 'Molly': 'English',
...: 'Sam': 'History'})
...: kelly_classes = pd.Series(['Philosophy', 'Arts', 'Math'], index=['Kelly', 'Kelly', 'Kelly'])
In [15]: students_classes.append(kelly_classes)
C:\Users\paul\AppData\Local\Temp\ipykernel_6072\990183765.py:1: FutureWarning: The series.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
students_classes.append(kelly_classes)
Out[15]:
Alice Physics
Jack Chemistry
Molly English
Sam History
Kelly Philosophy
Kelly Arts
Kelly Math
dtype: object
Under the covers, append
method uses _append
, which works without raising the warning:
In [16]: students_classes._append(kelly_classes)
Out[16]:
Alice Physics
Jack Chemistry
Molly English
Sam History
Kelly Philosophy
Kelly Arts
Kelly Math
dtype: object
And using the recommended concat
:
In [18]: pd.concat([students_classes,kelly_classes])
Out[18]:
Alice Physics
Jack Chemistry
Molly English
Sam History
Kelly Philosophy
Kelly Arts
Kelly Math
dtype: object
Python lists
have an efficient append
method. numpy
has a np.append
function which is a poorly named cover for calling np.concatenate
, and is often misused (it shouldn't be used iteratively). pandas
may be trying to avoid similar problems by getting rid of the append
method. With pd.concat
you can join many Series (or frames) at once, and aren't (as) tempted to use it in a loop.
Looking up the code for _append
(which is still in 2.0), I see it ends up using pd.concat
. So there's no value in using this 'work-around'. Use concat
as recommended.
Upvotes: 17