I have a pandas dataframe with following shape
open_year, open_month, type, col1, col2, ....
我希望能找到每个(年,月)中的最高类型,因此我首先要找到每个(年,月)中每种类型的数量。
freq_df = df.groupby(['open_year','open_month','type']).size().reset_index()
freq_df.columns = ['open_year','open_month','type','count']
我想找出每个(年_月)基于它们的频率(例如计数)的前n种类型。我该如何做呢?
我可以使用nlargest
,但我缺少类型。
freq_df.groupby(['open_year','open_month'])['count'].nlargest(5)
但我缺少列 类型