pandas 熊猫:选择两列不同的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48751140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:09:50  来源:igfitidea点击:

Pandas: select rows where two columns are different

pythonpandas

提问by user1670773

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 不相同的行。所以预期的输出是

a  b  c 
0  2  74
1  4  44

How can I do this?

我怎样才能做到这一点?

回答by Scott Boston

I am a fan of readability, use query:

我是可读性的粉丝,请使用query

df.query('a != b')

Output:

输出:

   a  b   c
1  0  2  74
3  1  4  44

回答by jpp

Try this:

尝试这个:

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

回答by zipa

Just use:

只需使用:

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

回答by YOBEN_S

By using nunique

通过使用 nunique

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