在 pandas 中的groupby和nunique之后过滤数据帧 [英] filter dataframe after groupby and nunique in pandas
本文介绍了在 pandas 中的groupby和nunique之后过滤数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了df.groupby("item")["variable"].nunique()
,它返回每个项目对象的唯一计数.
i tried df.groupby("item")["variable"].nunique()
and it returns a unique count of every item object.
我想过滤以仅返回变量"的计数> 3,条件取决于Groupby项...是否有方法?
i want to filter to only return the count of "variable" > 3 conditional on Groupby item... is there a method?
推荐答案
当您希望将groupby
映射到输入的每一行时,请考虑transform
:
When you want the groupby
to be mapped to every row of the input, think about transform
:
df = df[df.groupby("item")["variable"].transform('nunique') > 3]
这篇关于在 pandas 中的groupby和nunique之后过滤数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文