You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In [32]: df = DataFrame({'Sales' : [100,120,220], 'Month' : ['January','January','January'], 'Year' : [2013,2014,2013]})
In [33]: df
Out[33]:
Month Sales Year
0 January 100 2013
1 January 120 2014
2 January 220 2013
In [34]: df['Month'] = df['Month'].astype('category').cat.set_categories(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'])
In [35]: df.dtypes
Out[35]:
Month category
Sales int64
Year int64
dtype: object
In [36]: df['Month']
Out[36]:
0 January
1 January
2 January
Name: Month, dtype: category
Categories (12, object): [January < February < March < April ... September < October < November < December]
In [37]: df.pivot_table(values="Sales", index="Month")
Out[37]:
Month
January 146.666667
February NaN
March NaN
April NaN
May NaN
June NaN
July NaN
August NaN
September NaN
October NaN
November NaN
December NaN
Name: Sales, dtype: float64
In [38]: df.pivot_table(values="Sales", index="Month").index
Out[38]: Index([u'January', u'February', u'March', u'April', u'May', u'June', u'July', u'August', u'September', u'October', u'November', u'December'], dtype='object')
In [39]: result = df.pivot_table(values='Sales', index="Month", columns="Year", aggfunc="sum")
In [40]: result
Out[40]:
Year 2013 2014
Month
April NaN NaN
August NaN NaN
December NaN NaN
February NaN NaN
January 320 120
July NaN NaN
June NaN NaN
March NaN NaN
May NaN NaN
November NaN NaN
October NaN NaN
September NaN NaN
The text was updated successfully, but these errors were encountered:
xref #8860, soln might be the same
from SO
The text was updated successfully, but these errors were encountered: