编程语言
首页 > 编程语言> > python – TypeError:pivot_table()获取关键字参数’values’的多个值

python – TypeError:pivot_table()获取关键字参数’values’的多个值

作者:互联网

我正在使用Python2.7.我正在学习熊猫并正在实施pivot_table.在实现pivot_table documentation中给出的示例时:

    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')

运行时,我收到以下错误:
TypeError:pivot_table()获取关键字参数’values’的多个值

任何人都可以告诉为什么会这样吗?

解决方法:

你需要删除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

标签:python,pandas,pivot-table
来源: https://codeday.me/bug/20191008/1870711.html