I have a dataset made up of 4 columns, a numerator denominator, country, and month. I am pivoting it to get months as columns, country as index, and values as sum(numerator)/sum(denominator). The only problem I get is that my columns are all out of order. How can I sort the columns so earlier months appear first? I tried table = table.sort_index(1) with no luck.
table = pd.pivot_table(df, values=['Numerator', 'Denominator'], index='Country',
                columns=['Month'], aggfunc=np.sum)
table = table['Numerator'] / table['Denominator']
Edit with full example and data:
Data:
Denominator,Numerator,Country,Month
10,4,USA,1-Jan
6,2,USA,1-Jan
10,1,Canada,1-Jan
9,2,Canada,1-Jan
6,4,Canada,1-Feb
4,3,Canada,1-Feb
Code:
import pandas as pd
import numpy as np
df = pd.read_csv('data.csv')
table = pd.pivot_table(df, values=['Numerator', 'Denominator'], index='Country',
                columns=['Month'], aggfunc=np.sum)
table = table['Numerator'] / table['Denominator']
print table
Output:
Month    1-Feb     1-Jan
Country                 
Canada     0.7  0.157895
USA        NaN  0.37500
Desired Output:
Month    1-Jan     1-Feb
Country                 
Canada     0.157895  0.7
USA        0.37500   NaN
 
    