pandas 类型错误:append() 缺少 1 个必需的位置参数:“其他”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54112105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: append() missing 1 required positional argument: 'other'
提问by Stacey
I have several dataframes (of the same shape) that I want to append creating one larger data-frame. Tee individual data-frames all have the type:
我有几个数据框(形状相同),我想附加它们以创建一个更大的数据框。Tee 单个数据框都具有以下类型:
C-Mastersheet.xlsx <class 'pandas.core.frame.DataFrame'>
D-Mastersheet.xlsx <class 'pandas.core.frame.DataFrame'>
L-Mastersheet.xlsx <class 'pandas.core.frame.DataFrame'>
and look like:
看起来像:
C-Mastersheet.xlsx
C-Mastersheet.xlsx
First Name Last name Dept Location Status Concat
0 Jo Jones Accounts Bristol Current JonesJo
1 Sid Smith Sales Hull New SmithSid
D-Mastersheet.xlsx
D-Mastersheet.xlsx
First Name Last name Dept Location Status Concat
0 Phil Evans Production Hull Current EvansPhil
1 Sarah Heath Marketing Bristol Current HeathSarah
2 Jane Hill Accounts Bristol Current HillJane
3 Amy Cooper Sales Hull Current CooperAmy
L-Mastersheet.xlsx
L-Mastersheet.xlsx
First Name Last name Dept Location Status Concat
0 Marcus Price Operations Hull Current PriceMarcus
1 Andrew King Design Bristol Current KingAndrew
2 Emma Lane Marketing Bristol Current LaneEmma
3 Brian Deen Accounts Bristol Current DeenBrian
4 Steve Hymans Design Bristol Current HymansSteve
I am trying to return the output:
我正在尝试返回输出:
First Name Last name Dept Location Status Concat
0 Jo Jones Accounts Bristol Current JonesJo
1 Sid Smith Sales Hull New SmithSid
2 Phil Evans Production Hull Current EvansPhil
3 Sarah Heath Marketing Bristol Current HeathSarah
4 Jane Hill Accounts Bristol Current HillJane
5 Amy Cooper Sales Hull Current CooperAmy
6 Marcus Price Operations Hull Current PriceMarcus
7 Andrew King Design Bristol Current KingAndrew
8 Emma Lane Marketing Bristol Current LaneEmma
9 Brian Deen Accounts Bristol Current DeenBrian
10 Steve Hymans Design Bristol Current HymansSteve
I am trying to do this using the follwing code whioch loops around a directory:
我正在尝试使用以下围绕目录循环的代码来执行此操作:
ConsolidatedData = pd.DataFrame
for i in os.listdir(os.chdir(returnsfolder)):
if i.endswith(".xlsx"):
)
rawFilePath = returnsfolder +'\'+ i
DeptReturn = openRawDeptReturn(rawFilePath)
ConsolidatedData = ConsolidatedData.append(DeptReturn,ignore_index=True)
I am however getting the following type error:
但是,我收到以下类型错误:
TypeError: append() missing 1 required positional argument: 'other'
I have not come across this before.
我以前没有遇到过这种情况。
回答by jpp
This is the problem:
这就是问题:
df = pd.DataFrame # returns class
df = df.append(DeptReturn) # TypeError: append() missing 1 required positional argument: 'other'
The reason behind the error is the firstargument of a method is the class instance. In this case, the class instance is inferred to be DeptReturn
and there is no 'other'
argument supplied.
错误背后的原因是方法的第一个参数是类实例。在这种情况下,类实例被推断为是DeptReturn
并且没有'other'
提供参数。
What you need is:
你需要的是:
df = pd.DataFrame() # returns class instance
df = df.append(DeptReturn) # returns instance with method applied
For the first argument we have the class instancedf
, since the method is being applied on that instance. The second argument is DeptReturn
.
对于第一个参数,我们有类instancedf
,因为该方法正在应用于该实例。第二个参数是DeptReturn
。
See also: What is the purpose of self?
另请参阅:self 的目的是什么?