Python 数据框按列值过滤行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44482095/
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-08-20 00:03:10  来源:igfitidea点击:

Dataframe filtering rows by column values

pythonpandasdataframefilter

提问by Seunghun Choi

I have a Dataframe df

我有一个数据框 df

       Num1   Num2 
one       1      0
two       3      2
three     5      4
four      7      6
five      9      8

I want to filter rows that have value bigger than 3 in Num1 and smaller than 8 in Num2.

我想过滤 Num1 中值大于 3 且 Num2 中值小于 8 的行。

I tried this

我试过这个

df = df[df['Num1'] > 3 and df['Num2'] < 8]

but the error occurred.

但发生了错误。

ValueError: The truth value of a Series is ambiguous.

ValueError:系列的真值不明确。

so I used

所以我用

df = df[df['Num1'] > 3]
df = df[df['Num2'] < 8]

I think the code can be shorter.

我认为代码可以更短。

Is there any other way?

有没有其他办法?

回答by jezrael

You need add ()because operator precedence with bit-wise operator &:

您需要添加,()因为运算符优先级为按位运算符&

df1 = df[(df['Num1'] > 3) & (df['Num2'] < 8)]
print (df1)
       Num1  Num2
three     5     4
four      7     6

Better explanation is here.

更好的解释是here

Or if need shortest code use query:

或者如果需要最短的代码使用query

df1 = df.query("Num1 > 3 and Num2 < 8")
print (df1)
       Num1  Num2
three     5     4
four      7     6


df1 = df.query("Num1 > 3 &  Num2 < 8")
print (df1)
       Num1  Num2
three     5     4
four      7     6

回答by Willem Van Onsem

Yes, you can use the &operator:

是的,您可以使用&运算符:

df = df[(df['Num1'] > 3) & (df['Num2'] < 8)]
#                        ^ & operator

This is because andworks on the truthiness valueof the two operands, whereas the &operator can be defined on arbitrary data structures.

这是因为and适用于两个操作数的真实值,而&运算符可以定义在任意数据结构上。

The brackets are mandatory here, because &binds shorter than >and <, so without brackets, Python would read the expression as df['Num1'] > (3 & df['Num2']) < 8.

括号在这里是强制性的,因为&绑定比>and短<,所以如果没有括号,Python 会将表达式读作df['Num1'] > (3 & df['Num2']) < 8.

Note that you can use the |operator as a logical or.

请注意,您可以将|运算符用作逻辑或。