python - 类型错误 : pivot_table() got multiple values for keyword argument 'values'

标签 python pandas pivot-table

我正在使用 Python2.7。我正在学习 Pandas 并正在实现数据透视表。在实现 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')

运行时,出现以下错误: 类型错误:pivot_table() 为关键字参数“值”获取了多个值

谁能告诉我为什么会这样?

最佳答案

您需要删除 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 - 类型错误 : pivot_table() got multiple values for keyword argument 'values' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42134486/

相关文章:

Python 对数据帧进行采样

google-sheets - 从 Google 表格数据透视表中删除零行

mysql - 限制 MYSQL 数据透视表中显示的列

Python Pandas 使用换行符拆分从文件创建数据框?

mysql - 如何在 MySQL 中返回数据透视表输出?

Python包安装错误-找不到py_compiler msvc

python - 为什么我无法在提交给 ProcessPoolExecutor 的可调用对象中创建/访问 future?

python - 用Python求解积分微分耦合方程组

python - 在带有 cPickle 的 python 3.7 上使用 python 2.7 代码时出现 UnicodeDecodeError

python - 如何在特定级别重新排序多索引数据框列