Pandas: Selecting rows based on value counts of a particular column

Viewed 22940

Whats the simplest way of selecting all rows from a panda dataframe, who's sym occurs exactly twice in the entire table? For example, in the table below, I would like to select all rows with sym in ['b','e'], since the value_counts for these symbols equal 2.

df=pd.DataFrame({'sym':['a', 'b', 'b', 'c', 'd','d','d','e','e'],'price':np.random.randn(9)})

                     price sym
    0              -0.0129   a
    1              -1.2940   b
    2               1.8423   b
    3              -0.7160   c
    4              -2.3216   d
    5              -0.0120   d
    6              -0.5914   d
    7               0.6280   e
    8               0.5361   e

df.sym.value_counts()
Out[237]: 
d    3
e    2
b    2
c    1
a    1
2 Answers

You can use map, which should be faster than using groupby and transform:

df[df['sym'].map(df['sym'].value_counts()) == 2]

e.g.

%%timeit
df[df['sym'].map(df['sym'].value_counts()) == 2]
Out[1]:
1.83 ms ± 23.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit
df[df.groupby("sym")["sym"].transform('size') == 2]
Out[2]:
2.08 ms ± 41.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Related