删除数据帧中标签计数大于2的行

2024-10-01 09:41:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据框,看起来像这样:

**ReviewerID**          **ReviewText**        **Categories**       **ProductId**

    1212                   good product         Mobile               14444425
    1233                   will buy again       drugs                324532
    5432                   not recomended       Mobile               789654123

我要删除类别值至少两次未出现的所有行。 结果数据帧应如下所示:

**ReviewerID**       **ReviewText**        **Categories**       **ProductId**

    1212                   good product         Mobile               14444425
    5432                   not recomended       Mobile               789654123 

我刚接触Python和熊猫,非常感谢您的帮助


Tags: 数据notbuyproductmobile类别willcategories
1条回答
网友
1楼 · 发布于 2024-10-01 09:41:52

我想你需要^{}

print df.groupby('Categories').filter(lambda x: len(x) > 1)
   ReviewerID      ReviewText Categories  ProductId
0        1212    good product     Mobile   14444425
2        5432  not recomended     Mobile  789654123

Docs

相关问题 更多 >