简体   繁体   中英

Order a pivot table by data in pandas

I have the following dataframe.

在此处输入图像描述

I need a pivot table in the below format:

Category  Jul-18   Aug-18 Sep-18 Oct-18 Nov-18  Dec-18 Jan-19 Feb-19 Mar-19

Batter     55000    80000  5500   92000  20000  .        .      .     .  
Home Food  80000    92000  55000  80000  5500   .        .      .     .   

I achieved this by using the pivot table function of pandas:

pd.pivot_table(data, values = ['Revenue'],index = ['Category'],columns = [data.MonthYear], aggfunc= {'Revenue':np.sum},fill_value=0).sort_index(axis=1,level=1)

The problem with this is that the Month year column is getting sorted alphabetically and not according to the date.

在此处输入图像描述

I tried to change the MonthYear column to date time format using this:

data['MonthYear'] = pd.to_datetime(data['MonthYear'], format='%b-%Y').dt.to_period('M')

This solved the sorting issue, but the visual is the problem this time:

在此处输入图像描述

I need the columns to be in the format %b-%y (Jan-20, Feb-20 etc.) and also be ordered according to the date. Any help here please?

You can change format of month periods after pivoting like, also remove lists [] from pivot_table for avoid MultiIndex :

data['MonthYear'] = pd.to_datetime(data['MonthYear'], format='%b-%Y').dt.to_period('M')

df = pd.pivot_table(data, 
                    values = 'Revenue',
                    index = 'Category',
                    columns = 'MonthYear',
                    aggfunc='sum',
                    fill_value=0)

df.columns = df.columns.strftime('%b-%Y')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM