Francisco García Francisco García -4 years ago 183
Python Question

Remove values that appear only once in a DataFrame column

I have a dataframe with different values in column

x
. I want to drop values that appear only once in a column.

So this:

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


Should turn into this:

x
2 30
3 30
4 40
5 40


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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download