1

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.

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
bzdhss
  • 13
  • 3

1 Answers1

1

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252