使用 pandas 嵌套 where 语句? [英] Nested where statement using pandas?
问题描述
我正在尝试使用 python 在 PowerBI 中运行一个简单的查询.遗憾的是,PowerBI 不支持大多数 python 库,所以我仅限于 pandas 和 numpy.数据集是一组正在筹备中或活动中的项目.我想根据一组或条件将数据集过滤到管道中的行.所以它想看
I am attempting to run a simple query in PowerBI using python. Sadly, most python libraries are not supported in PowerBI, so I'm limited to pandas and numpy. The dataset is a set of projects that are either in pipeline or active. I want to filter the dataset down to rows that are just in pipeline based on a set of or conditions. So it would like look
dataframe = pd.DataFrame(where project = 'Pipeline'), 过滤管道启动的其他条件)
dataframe = pd.DataFrame(where project = 'Pipeline'), set of other conditions to filter pipeline launches by)
这在 python 中是否可行,类似于嵌套的 where 语句?
Is that possible in python, similar to a nested where statement?
推荐答案
如果一个变量中的所有条件都这样做:
If all conditions in one variable do:
df [ df.column_name.isin([value_1, value_2, value_n]) ]
如果条件在多列中做
df [ (df.column_1 == "value_1") & (df.column_2 == "value_2") & (df.column_n.isin([val_1, val_2, val_3])) ]
注意:&
表示 AND
,所以如果(左右)都为 True,则结果为 True
,否则结果为 False
.
Note:
&
means AND
, so if both (left and right) are True the result is True
, else result False
.
如果你需要OR
条件使用|
.
这篇关于使用 pandas 嵌套 where 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!