How to fix AttributeError: 'Series' object has no attribute 'append'

问题描述 投票:0回答:1

嗨,我还在用 python 学习 pandas 和 numpy

我从电子学习中了解到,您可以将 2 个系列与追加结合起来,但是当我尝试它时它给我错误

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

它给我这样的错误

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

pandas numpy attributes append series
1个回答
0
投票

pandas 的更新可能改变了语法,但您可能正在寻找:

all_students_classes = students_classes._append(kelly_classes)

(快速提示,您可以通过右键单击 pd.Series 并选择“转到定义”来检查 VS 代码中的类定义,然后您至少可以看到一些定义的方法)。

© www.soinside.com 2019 - 2024. All rights reserved.