Cks
Cks

Reputation: 314

how to select multiple rows as a group based on a column condition in pandas

How can we select all the rows in different matching groups based on a column condition

Data:

**A B   C   D** 

101 1   1   FALSE

101 1   2   FALSE

101 1   3   FALSE

101 2   1   FALSE

101 2   2   FALSE

101 2   3   FALSE

101 2   4   TRUE

102 1   1   FALSE

102 1   2   FALSE

102 1   3   FALSE

102 2   1   FALSE

102 2   2   FALSE

102 2   3   TRUE

Expected Output:

**A B   C   D**

101 2   1   FALSE

101 2   2   FALSE

101 2   3   FALSE

101 2   4   TRUE

102 2   1   FALSE

102 2   2   FALSE

102 2   3   TRUE

I need all the rows where B = (B when D = True)

df.loc[df.groupby(["A"])[df_rvtlt['D'] == True]]

Upvotes: 0

Views: 1288

Answers (2)

BENY
BENY

Reputation: 323326

Here is one way from transform

df[df.groupby(['A','B']).D.transform('mean')>0]
Out[256]: 
      A  B  C      D
3   101  2  1  False
4   101  2  2  False
5   101  2  3  False
6   101  2  4   True
10  102  2  1  False
11  102  2  2  False
12  102  2  3   True

using any

df[df.groupby(['A','B']).D.transform('any')]

Upvotes: 1

rafaelc
rafaelc

Reputation: 59274

IIUC, use groupby+filter

df.groupby(['A', 'B']).filter(lambda s: s['D'].any())

    A   B   C   D
3   101 2   1   False
4   101 2   2   False
5   101 2   3   False
6   101 2   4   True
10  102 2   1   False
11  102 2   2   False
12  102 2   3   True

Upvotes: 1

Related Questions