I am trying to create a pivot table from a Dataframe using Pandas. Given below is the view of my Dataframe.
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
I am trying to create a pivot and save the output to an excel file
Summary = pd.pivot_table(df, values=['total'], index=['category'], columns='date')
Summary.to_excel(writer, sheet_name='Summary')
I get the below error
KeyError : 'total'
Could anyone guide me where am I gong wrong with this. Thanks
Updating on the datatype:
category object
date object
type1 int64
type2 int64
total float64
dtype: object
Output of 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
Summary = pd.pivot_table(df, values='total', index='category', columns='date')print (df.columns.tolist())Summary = pd.pivot_table(df, values='total', index='category', columns='date')then no problem?Summary = pd.pivot_table(df, values='total', index='category', columns='date').reset_index()