0

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.

Lucas Schwartz
  • 174
  • 1
  • 13

2 Answers2

2

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')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Thank you for the response. If I just simply would like to execute 'print('Hello World') without creating a new column as soon as the desired condition is fulfilled then how would I do that? – Lucas Schwartz Oct 05 '21 at 11:58
  • @LucasSchwartz - Answer was edited. – jezrael Oct 05 '21 at 12:00
1

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

If you want to print if any row meets conditions you can try this:

def prt(row):
    if row['condition1'] == 'ON' \
    and row['condition2'] > 15 \
    and row['condition3'] == 'OFF':
        return True
    return False
chk = df.apply(lambda row: prt(row),axis=1)
if chk.any():
    print('Hello world')
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • 1
    Check [this](https://stackoverflow.com/questions/54432583/when-should-i-not-want-to-use-pandas-apply-in-my-code/54432584#54432584) – jezrael Oct 05 '21 at 11:41
  • 1
    @jezrael I definitely approve vectorize is better but is good OP see all approach in future maybe he can decide for all problem use vectorized or apply If I don't post this answer this conversion never exist. – I'mahdi Oct 05 '21 at 11:43
  • ya, it is most problem of people starting with pandas - using apply if not necessary like here. So No problem if post solution, but always necessary warning - dont use it, because slow, not vectorized... – jezrael Oct 05 '21 at 11:45
  • why use vectorize solution in future and not today? – jezrael Oct 05 '21 at 11:46