Home > Software engineering >  using a pandas dataframe to test for multiple "IF", "AND, "ELIF" conditions
using a pandas dataframe to test for multiple "IF", "AND, "ELIF" conditions

Time:10-05

I have this dataframe:

import pandas as pd
df = pd.DataFrame({'condition1': ['ON', 'OFF', 'ON'], 
                   'condition2': [10, 15, 20],
                   'condition3' : ['OFF', 'ON', 'OFF']})

Output:

  condition1  condition2 condition3
0         ON          10        OFF
1        OFF          15         ON
2         ON          20        OFF

Now lets say, I would like to execute "print('Hello World')" IF 'condition1' is 'ON', 'condition2' is > 15, and 'condition3' is 'OFF'.

So based on the dataframe above 'Hello World' Would be printed when the 3rd row is evaluated as the set criteria have been fulfilled.

Please recommend a way to achieve the above goal, and provide a code snippet to demonstrate the solution.

CodePudding user response:

You can use apply on row like below: (this approach slower than vectorize like another answer)

def prt(row):
    if row['condition1'] == 'ON' \
    and row['condition2'] > 15 \
    and row['condition3'] == 'OFF':
        return ('Hello World!')
    return ''
df.apply(lambda row: prt(row),axis=1)

Output:

0                
1                
2    Hello World!
dtype: object

CodePudding user response:

First is best forget for looping in apply or for, because vectorized solutions exist:

#create mask
m = (df['condition1'] == 'ON') & (df['condition2'] > 15) & (df['condition3'] == 'OFF')

#if need new column
df['new'] = np.where(m, 'Hello World', '')
print (df)
  condition1  condition2 condition3          new
0         ON          10        OFF             
1        OFF          15         ON             
2         ON          20        OFF  Hello World

If need execute print ('Hello World') if exist in dataframe use:

if m.any():
    print ('Hello World')
  • Related