简体   繁体   中英

Pandas: select rows where two columns are different

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?

I am a fan of readability, use query :

df.query('a != b')

Output:

   a  b   c
1  0  2  74
3  1  4  44

尝试这个:

df.loc[df['a'] != df['b']]

By using nunique

df.loc[df[['a','b']].nunique(1)>1]
Out[335]: 
   a  b   c
1  0  2  74
3  1  4  44

只需使用:

df.loc[df['a']!=df['b']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM