[英]how to filter data frame with multiple conditions in python
I have the following data frame我有以下数据框
Time EQU ID VALUE
2022-05-03 202201 AB0125 0
2022-05-03 202201 AB0120 0
2022-05-03 202201 AB0121 0
2022-05-03 202201 AB0122 1
2022-05-03 202201 AB0123 0
2022-05-03 202201 AB0124 0
2022-05-03 202201 AB0125 0
2022-05-03 202201 DE0112 120
2022-05-03 202201 FE0253 23
2022-05-03 202201 GE0223 50
2022-05-03 202201 WW0365 10
I want to get data if either of AB0120,AB0121,AB0122,AB0123,AB0124,AB0125 is 1. How can I filter using python?如果 AB0120、AB0121、AB0122、AB0123、AB0124、AB0125 中的任何一个为 1,我想获取数据。如何使用 python 进行过滤?
We can use str.contains
here:我们可以在这里使用
str.contains
:
df_out = df[(df["ID"].str.contains(r'^ABC012[0-5]$') & (df["VALUE"] == 1)]
Use regex string contains method with a search to match any string from your desired strings使用 regex string contains 方法进行搜索以匹配所需字符串中的任何字符串
conditions=["AB0120","AB0121","AB0122","AB0123","AB0124","AB0125"]
df[(df.ID.str.contains("|".join(""), na=False)) & (df.VALUE==1)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.