按频率过滤 Pandas 数据帧

Filtering a Pandas Dataframe by frequency

我有一个大约有 50k 行和 15 列的数据集。 我只需要选择遵循规则的某些行:'if the string in column "C" appears more than 20 times in the dataset (within that column), pick that row'。基本上是根据某列的模式过滤掉

最后,我希望得到一个包含大约 5k 行(和 15 列)的数据集。

我尝试通过对列使用 value_counts() 来执行此操作,但是我无法将其余信息与特定行匹配。

非常感谢您!

让我们尝试 groupby().transform 和布尔索引:

df.loc[df.groupby('C')['C'].transform('size') >= 20]