python - Pandas Pivot Table alphabetically sorts categorical data (incorrectly) when adding columns parameter -
I was annoyed with Pandas Pivot function. I am trying to pitch sales data during months and years - - Name of the month - Year One - 100 - January - 2013 One - 120 - January - 2014 b - 220 - January - 2013 <
Customers: Datasets are as follows / Month>]. ('January', 'February', 'March', 'Apr', 'May', 'Jun', 'Jul', 'Aug' ']
When I use the Function :
pt = dataset.pivot_table (value = "sale", index = "month")
I get the result of the hope
month January 3,620,302.79 February 3,775,507.25 March 4,543,839.69
However when I sorted the pivot month alphabetical order throughout the year and month .
print dataset.pivot_table (value = 'sale', index = "month", column = "year", a Ggfunc = "yoga") year 2011 2012 2013 months April 833692.19 954483.28 1,210,847.85 1,210,926.61 August 722604.75 735078.52 879905.23 1,207,211.00 December 779873.51 1,053,441.71 1,243,745.73 Thank you, Frank < / P>
I
>
You are correct after pivot_table
, This will add 'month' again and thus will be described in alphabetical order as described. Fortunately you can always change your dataset ['month']
to pandas.datetime
and convert it back to the string after pivot_table
Indexing:.
Not the best solution, but this should do the trick (I use some random dummies):
Import pandas as pd ... # Dataset changed ['Month'] By the time of Pivot, pandas.datetime # will reindex Reddex according to the time period, hence the order order is pivoted = dataset. Pivot_table (index = pd.to_datetime (dataset ['month'] ), Column = 'year', \ values = 'sale', = 'yoga' aggfunc) 2012 2012 2014 pivoted month 2014-01-04 151 295 NaN 2014-02-04 279 128 NaN 2014-03- 04 218 24 4 NaN 2014-04-04 274 152 NaN 2014- 05-04 276 NaN 138 2014-06-04 223 NaN 209 ... # Then set the index again on the month string, "% B" means the month string " January "etc. Pivoted.index = [pd.datetime pivoted. Stacktime for Meter in Indx (M, Format = '% B')] Pivt Year 2012 2013 Jan 151 295 NN Feb 27 128 NAN 21 March 244 NAN April 274 152 NN 276 NAN 138 June 223 NAN 209 ...
Although you will remember the 'Month' index label, if you need it, then you need dataset ['month'] to the second column (which is < Code> M ) and you can convert it to datetime
, then click on pivot_table dataset.pivot_table (index = ['m', 'month'], ...)
< / Pre>
Comments
Post a Comment