Python Pandas:如何过滤存储在不同变量中的多个表达式的数据帧?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20262290/
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
Python Pandas: How to filter a dataframe with more than one expression stored in different variables?
提问by I want badges
I am building a multy purpose User Interface, and I am adding Pandas to it. For this, I need to form expressions by components (stored in variables) which are defined by users choices.
我正在构建一个多用途的用户界面,我正在向它添加 Pandas。为此,我需要通过由用户选择定义的组件(存储在变量中)来形成表达式。
All seems to work fine, but I got into a dead end. I want the user to be able to pick several expressions, and then concatenate them to form the new dataframe. If I only use one expression, everything will work:
一切似乎都很好,但我陷入了死胡同。我希望用户能够选择几个表达式,然后将它们连接起来形成新的数据框。如果我只使用一个表达式,一切都会奏效:
from pandas import read_csv
df = read_csv("SomeCsv.csv")
b= df[r'ID']
a=(b==r'p')
Value=df[a] #Works,returning the rows in df whichs column 'ID' equals r'p'
But if I want to include more expressions:
但如果我想包含更多表达式:
from pandas import read_csv
df = read_csv("SomeCsv.csv")
b= df[r'ID']
c=(b==r'p')
d=(b==r'ul')
a=c or d #Breaks at this line
Value=df[a] #Doesnt work. I would expect the rows in df whichs column 'ID' equals r'p' or 'ID' equals r'ul'
And throws the following error:
并抛出以下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Before asking, I tried all the .any and .all combinations of the expressions I could think of, and all of them failed.
在问之前,我尝试了所有我能想到的表达式的 .any 和 .all 组合,但都失败了。
How to filter this dataframe by columns matching more than one expression stored in variables?
如何通过匹配存储在变量中的多个表达式的列来过滤此数据框?
回答by Roman Pekar
Actually you can go with @miku answer, but in your case you also can use pandas.Series.isin()method:
实际上,您可以使用@miku 的答案,但在您的情况下,您也可以使用pandas.Series.isin()方法:
>>> df[df['ID'].isin(('p', 'ul'))]
回答by miku
As a newcomer to numpy I struggled a bit (no pun intended) about this too. I believe you want something like this:
作为 numpy 的新手,我对此也有点挣扎(不是双关语)。我相信你想要这样的东西:
>>> df[(df['ID'] == 'p') | (df['ID'] == 'ul')]
The expression must evaluate to a boolean (and the terms must be connected through bitwiseoperations), which then is used to mask or filter the corresponding elements.
表达式必须计算为布尔值(并且这些项必须通过按位运算连接 ),然后用于屏蔽或过滤相应的元素。
See also:
也可以看看:

