pandas 类型错误:pivot_table() 为关键字参数“values”获得了多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42134486/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:56:03  来源:igfitidea点击:

TypeError: pivot_table() got multiple values for keyword argument 'values'

pythonpandaspivot-table

提问by Kumar Gaurav

I am using Python2.7. I am learning pandas and was implementing pivot_table. While implementing the example given in pivot_table documentation:

我正在使用 Python2.7。我正在学习Pandas并正在实施 pivot_table。在实现 pivot_table文档中给出的示例时:

    raw_data = {'A':['foo','foo','foo','foo','foo','bar','bar','bar','bar'],
        'B':['one','one','one','two','two','one','one','two','two'],
        'C':['small','large','large','small','small','large','small','small','large'],
        'D':[1,2,2,3,3,4,5,6,7]}
    df = pd.DataFrame(raw_data)
    df.pivot_table(df,index = ['A','B'], values = 'D',columns = 'C', aggfunc = 'sum')

When run, I get the following error : TypeError: pivot_table() got multiple values for keyword argument 'values'

运行时,我收到以下错误:TypeError: pivot_table() got multiple values for keyword argument 'values'

Can anyone tell why this is happening ?

谁能告诉为什么会这样?

回答by jezrael

You need remove df:

你需要删除df

              #here      
df.pivot_table(df,index = ['A','B'], values = 'D',columns = 'C', aggfunc = 'sum')


a = df.pivot_table(index = ['A','B'], values = 'D',columns = 'C', aggfunc = 'sum')
print (a)
C        large  small
A   B                
bar one    4.0    5.0
    two    7.0    6.0
foo one    4.0    1.0
    two    NaN    6.0