Pandas: select rows where two columns are different

后端 未结 4 474
死守一世寂寞
死守一世寂寞 2020-12-19 09:15

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

相关标签:
4条回答
  • 2020-12-19 09:53

    Try this:

    df.loc[df['a'] != df['b']]
    
    0 讨论(0)
  • 2020-12-19 10:14

    I am a fan of readability, use query:

    df.query('a != b')
    

    Output:

       a  b   c
    1  0  2  74
    3  1  4  44
    
    0 讨论(0)
  • 2020-12-19 10:14

    By using nunique

    df.loc[df[['a','b']].nunique(1)>1]
    Out[335]: 
       a  b   c
    1  0  2  74
    3  1  4  44
    
    0 讨论(0)
  • 2020-12-19 10:14

    Just use:

    df.loc[df['a']!=df['b']]
    
    0 讨论(0)
提交回复
热议问题