pandas问题:如何对pivot_table中的行标签进行排序
各位大佬好,本人pandas小白一枚。对pandas中pivot_table()的一个问题:请问该如何将下面的pivot_table中的行标签进行按升序进行重排,也就是说实现 Month 这一行的标签以 1,2,3,……,12 的顺序呈现。有大佬知道如何实现吗?
代码:
tt = pd.pivot_table(df1, index = 'Year', columns = 'Month', values = 'TotalPrices', aggfunc = {'TotalPrices': np.sum},margins = False)
tt
运行结果如下:
Month 1 10 11 12 2 3 4 5 6 7 8 9
Year
2010 NaN NaN NaN -74729.12 NaN NaN NaN NaN NaN NaN NaN NaN
2011 -131363.05 -81895.5 -47720.98 -205089.27 -25519.15 -34201.28 -44600.65 -47202.51 -70569.78 -37919.13 -54330.8 -38838.51
用 sort_index 试试看
tt.sort_index(axis=1)
页:
[1]