我有一个具有以下形状的 Pandas 数据框
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']
然后我想根据每个(year_month)的频率(例如计数)找到前 n 个类型。我怎样才能做到这一点?
我可以用
nlargest
但我缺少类型freq_df.groupby(['open_year','open_month'])['count'].nlargest(5)
但我错过了专栏
type
最佳答案
我建议先按降序对您的计数进行排序,您可以调用 GroupBy.head
后-
(freq_df.sort_values('count', ascending=False)
.groupby(['open_year','open_month'], sort=False).head(5)
)
关于python - 如何在pandas中分组后从每组中选择前n行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50415632/