bzdhss
bzdhss

Reputation: 13

select elements in df pandas according to another column value in df

I have my df as following:

In [2]: df Out[2]:

    A  B  C
0  b1  1  0
1  b2  1  0
2  b1  3  1
3  b1  2  1
4  b2  2  1
5  b2  4  1

I want to use a pandas command to just select elements according the column B:

In [2]: df_new Out[2]:

   A  B  C
0  b1  1  0
1  b2  1  0
3  b1  2  1
4  b2  2  1

Cheers, Behzad.

Upvotes: 1

Views: 55

Answers (1)

jezrael
jezrael

Reputation: 863741

Use DataFrame.duplicated for boolean mask by 2 columns and keep=False for return all dupes and filter by boolean indexing:

df = df[df.duplicated(['B', 'C'], keep=False)]
print (df)
    A  B  C
0  b1  1  0
1  b2  1  0
3  b1  2  1
4  b2  2  1

Upvotes: 1

Related Questions