首页 > 解决方案 > Pandas groupby 仅在观察数超过 X 时显示

问题描述

我有以下代码,我将列预测与列置信度的平均值分组,然后按最高的 10 个进行排序。我需要限制每组内的观察数量,因为有些组非常小,因此不应该出现在上面。

prediction_difficulty = master.groupby(['prediction'])['confidence'].mean().reset_index() prediction_difficulty.sort_values('confidence', ascending=False)[:10]

标签: pythonpandaspandas-groupby

解决方案


我相信需要GroupBy.head

prediction_difficulty.sort_values('confidence',ascending=False).groupby('prediction').head(10)

推荐阅读