Home > other >  how to filter data frame with multiple conditions in python
how to filter data frame with multiple conditions in python

Time:09-16

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?

CodePudding user response:

We can use str.contains here:

df_out = df[(df["ID"].str.contains(r'^ABC012[0-5]$') & (df["VALUE"] == 1)]

CodePudding user response:

Use regex string contains method with a search to match any string from your desired strings

conditions=["AB0120","AB0121","AB0122","AB0123","AB0124","AB0125"]
df[(df.ID.str.contains("|".join(""), na=False)) & (df.VALUE==1)]
  • Related