11

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
  • 967
  • 4
  • 12
  • 23
  • `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

24

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
  • 147,308
  • 15
  • 139
  • 187
7

Try this:

df.loc[df['a'] != df['b']]
jpp
  • 159,742
  • 34
  • 281
  • 339
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
  • 317,841
  • 20
  • 164
  • 234
1

Just use:

df.loc[df['a']!=df['b']]
zipa
  • 27,316
  • 6
  • 40
  • 58