按熊猫分组并排序
作者:互联网
我已经按功能分组,我想按时间顺序按月份排序,该怎么办?当前,该功能按字母顺序对月份进行排序:
func = {'Predictions':['count','mean','median']}
table1 = df.groupby(['FLAG','MONTH']).agg(func)
表格1
Predictions
count mean median
FLAG MONTH
0 Apr 49812 106.458209 75.325309
Aug 44514 93.718901 74.485782
Feb 51583 98.921119 74.199794
Jan 54837 100.381814 74.682187
Jul 49873 100.621877 73.233328
Jun 47950 103.688532 74.150171
Mar 52816 106.094774 75.104832
May 49404 106.847784 75.844241
Oct 41828 94.744952 76.178077
Sep 41074 96.430351 75.335261
1 Apr 83377 285.631679 144.582569
Aug 66285 217.619038 127.087037
Feb 79693 310.919925 180.507922
Jan 64730 290.113451 137.291571
Jul 105213 298.337893 146.956319
Jun 90305 312.484185 136.222903
Mar 97274 308.013477 165.752471
May 87927 310.162600 142.350688
Oct 47064 258.213619 85.445310
Sep 47337 240.361602 84.597842
谢谢你的帮助!
解决方法:
您可以使用reindex
:
#rewrite code for remove MultiIndex in columns
table1 = df.groupby(['FLAG','MONTH'])['Predictions'].agg(['count','mean','median'])
months = ['Jan', 'Feb', 'Mar', 'Apr','May','Jun', 'Jul', 'Aug','Sep', 'Oct', 'Nov', 'Dec']
df = table1.reindex(months, level=1)
print (df)
count mean median
FLAG MONTH
0 Jan 54837 100.381814 74.682187
Feb 51583 98.921119 74.199794
Mar 52816 106.094774 75.104832
Apr 49812 106.458209 75.325309
May 49404 106.847784 75.844241
Jun 47950 103.688532 74.150171
Jul 49873 100.621877 73.233328
Aug 44514 93.718901 74.485782
Sep 41074 96.430351 75.335261
Oct 41828 94.744952 76.178077
1 Jan 64730 290.113451 137.291571
Feb 79693 310.919925 180.507922
Mar 97274 308.013477 165.752471
Apr 83377 285.631679 144.582569
May 87927 310.162600 142.350688
Jun 90305 312.484185 136.222903
Jul 105213 298.337893 146.956319
Aug 66285 217.619038 127.087037
Sep 47337 240.361602 84.597842
Oct 47064 258.213619 85.445310
标签:pandas-groupby,pandas,python,sorting 来源: https://codeday.me/bug/20191110/2014641.html