Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

And fastest solution with transform and 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...