I'm trying to add a summed column to a pivoted dataframe but keep getting a data parse error.
mydata = [{'amount': 3200, 'close_date':'2013-03-31', 'customer': 'Customer 1',},
{'amount': 1430, 'close_date':'2013-11-30', 'customer': 'Customer 2',},
{'amount': 4320, 'close_date':'2014-03-31', 'customer': 'Customer 3',},
{'amount': 2340, 'close_date':'2015-05-18', 'customer': 'Customer 4',},
{'amount': 4320, 'close_date':'2015-06-29', 'customer': 'Customer 5',},]
df = pd.DataFrame(mydata)
>>> df.dtypes
amount int64
close_date object
customer object
dtype: object
I convert to dates and then display as quarters
df.close_date = pd.to_datetime(df.close_date)
>>> df.dtypes
amount int64
close_date datetime64[ns]
customer object
dtype: object
df.close_date = df.close_date.dt.to_period('Q')
I then pivot:
pivot = pd.pivot_table(df,index='customer',columns='close_date')
Finally, I want to sum the rows and the columns but my this piece of code won't seem to add a column...
pivot['sum'] = pivot.sum(axis=1)
What am I doing wrong?