9

Suppose I have a dataframe as below

a  b  c  
1  1  45
0  2  74
2  2  54
1  4  44

Now I want the rows where column a and b are not same. So the expected outpu is

a  b  c 
0  2  74
1  4  44

How can I do this?

user1670773
  • 877
  • 3
  • 11
  • 20
  • `df[df['a'] != df['b']]`? – pault Feb 12 '18 at 16:32
  • 2
    possible duplicate (generalisation): https://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas – jpp Feb 12 '18 at 16:36

4 Answers4

21

I am a fan of readability, use query:

df.query('a != b')

Output:

   a  b   c
1  0  2  74
3  1  4  44
Scott Boston
  • 133,446
  • 13
  • 126
  • 161
6

Try this:

df.loc[df['a'] != df['b']]
jpp
  • 147,904
  • 31
  • 244
  • 302
3

By using nunique

df.loc[df[['a','b']].nunique(1)>1]
Out[335]: 
   a  b   c
1  0  2  74
3  1  4  44
BENY
  • 296,997
  • 19
  • 147
  • 204
2

Just use:

df.loc[df['a']!=df['b']]
zipa
  • 26,044
  • 6
  • 38
  • 55