创建数据透视表时出现Pandas错误(KeyError)

时间:2018-10-12 09:18:47

标签: python pandas pivot-table keyerror

我正在尝试使用Pandas从数据框创建数据透视表。下面是我的数据框的视图。

category,date,type1,type2,total
PROD_A,2018-10-01,2,2,4
PROD_A,2018-10-02,2,0,2
PROD_B,2018-10-01,0,0,0
PROD_A,2018-10-03,0,0,0

我正在尝试创建数据透视表并将输出保存到Excel文件中

Summary = pd.pivot_table(df, values=['total'], index=['category'], columns='date')

Summary.to_excel(writer, sheet_name='Summary')

我收到以下错误

KeyError : 'total'

有人可以指导我我在哪里犯错吗?谢谢

更新数据类型:

category   object
date       object
type1      int64
type2      int64
total      float64
dtype:     object

df.head()的输出:

category,date,type1,type2,total
PROD_A,2018-10-01,2,2,4
PROD_A,2018-10-02,2,0,2
PROD_B,2018-10-01,0,0,0
PROD_A,2018-10-03,0,0,0
PROD_B,2018-10-03,2,3,5

1 个答案:

答案 0 :(得分:1)

问题是['total'],它会在列中创建MultiIndex

Summary = pd.pivot_table(df, values=['total'], index=['category'], columns='date')

print (Summary)

              total                      
date     2018-10-01 2018-10-02 2018-10-03
category                                 
PROD_A          4.0        2.0        0.0
PROD_B          0.0        NaN        NaN

解决方案是使用将其删除:

Summary = pd.pivot_table(df, values='total', index='category', columns='date')
print (Summary)
date      2018-10-01  2018-10-02  2018-10-03
category                                    
PROD_A           4.0         2.0         0.0
PROD_B           0.0         NaN         NaN

最后按reset_index将索引转换为列:

Summary = (pd.pivot_table(df, values='total', index='category', columns='date')
             .reset_index(drop=True))
print (Summary)
date  2018-10-01  2018-10-02  2018-10-03
0            4.0         2.0         0.0
1            0.0         NaN         5.0