您可以使用SeriesGroupBy.nlargest
:
print (grouped.groupby(level='yearmonth').nlargest(3).reset_index(level=0, drop=True)) yearmonth product 201601 E 180 A 100 B 90 201602 F 220 A 200 C 120 201603 B 120 C 110 D 110 Name: val, dtype: int64
您也可以将to_datetime
with与to_period
转换为year-month
期间:
print (df) date product Volume 0 20160101 A 10 1 20160101 B 5 2 20160101 C 10 3 20160101 D 5 4 20160102 A 20 5 20160102 A 10 6 20160102 B 5 7 20160102 C 10 8 20160102 D 5 9 20160328 A 20 10 20160328 C 100 11 20160328 B 20 12 20160328 D 20 13 20160330 D 20
grouped = df.groupby([pd.to_datetime(df.date, format='%Y%m%d').dt.to_period('M'), 'product'])['Volume'].sum() print (grouped) date product 2016-01 A 40 B 10 C 20 D 10 2016-03 A 20 B 20 C 100 D 40 Name: Volume, dtype: int64 print (grouped.groupby(level='date').nlargest(3).reset_index(level=0, drop=True)) date product 2016-01 A 40 C 20 B 10 2016-03 C 100 D 40 A 20 Name: Volume, dtype: int64