Home > Software design >  Pandas: OR in a for loop
Pandas: OR in a for loop

Time:07-18

I have to write a condition for my dataframe like this:

df = df.loc[(df['Threshold_cyl_0'] >= 0.2) |
            (df['Threshold_cyl_1'] >= 0.2) |
            (df['Threshold_cyl_2'] >= 0.2) |
            (df['Threshold_cyl_3'] >= 0.2) |
            (df['Threshold_cyl_4'] >= 0.2) |
            (df['Threshold_cyl_5'] >= 0.2)] 

Do you know how i can write this code when i have a variable number of conditions (e.g. Threshold_cyl_n could be smaller or larger than 5)?

Thanks.

CodePudding user response:

You can compare a DataFrame and use any as OR:

cols = ['Threshold_cyl_0', 'Threshold_cyl_1', 'Threshold_cyl_2',
        'Threshold_cyl_3', 'Threshold_cyl_4', 'Threshold_cyl_5']

df = df.loc[df[cols].ge(0.2).any(1)]

Alternative if the target columns are all that contain "Threshold_cyl_":

df = df.loc[df.filter(like='Threshold_cyl_').ge(0.2).any(1)]
  • Related