删除在DataFrame列中仅出现一次的值 [英] Remove values that appear only once in a DataFrame column

查看:516
本文介绍了删除在DataFrame列中仅出现一次的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在x列中有一个具有不同值的数据框.我想删除仅在列中出现一次的值.

I have a dataframe with different values in column x. I want to drop values that appear only once in a column.

所以这个:

   x
1 10
2 30
3 30
4 40
5 40
6 50

应该变成这样:

   x
2 30
3 30
4 40
5 40

我想知道是否有办法做到这一点.

I was wondering if there is a way to do that.

推荐答案

您可以使用 groupby transform :

You can easily get this by using groupby and transform :

In [1]: import pandas as pd

In [2]: df = pd.DataFrame([10, 30, 30, 40, 40, 50], columns=['x'])

In [3]: df = df[df.groupby('x').x.transform(len) > 1]

In [4]: df
Out[4]: 
    x
1  30
2  30
3  40
4  40

这篇关于删除在DataFrame列中仅出现一次的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆