pandas :根据特定列的值计数选择行 [英] Pandas: Selecting rows based on value counts of a particular column

查看:71
本文介绍了 pandas :根据特定列的值计数选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从熊猫数据框中选择所有行的最简单方法是什么?谁的符号在整个表中恰好出现两次?例如,在下表中,我想选择在['b','e']中带有sym的所有行,因为这些符号的value_counts等于2.

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

推荐答案

我认为您可以使用length == 2的.html"rel =" noreferrer> filter 值:

I think you can use groupby by column sym and filter values with length == 2:

print df.groupby("sym").filter(lambda x: len(x) == 2)
      price sym
1  0.400157   b
2  0.978738   b
7 -0.151357   e
8 -0.103219   e

第二个解决方案使用 isin 布尔索引:

Second solution use isin with boolean indexing:

s = df.sym.value_counts()

print s[s == 2].index
Index([u'e', u'b'], dtype='object')

print df[df.sym.isin(s[s == 2].index)]
      price sym
1  0.400157   b
2  0.978738   b
7 -0.151357   e
8 -0.103219   e

使用 boolean indexing :

print (df[df.groupby("sym")["sym"].transform('size') == 2])
    price sym
1 -1.2940   b
2  1.8423   b
7  0.6280   e
8  0.5361   e

这篇关于 pandas :根据特定列的值计数选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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