Home > Blockchain >  Python Pandas IF ELSE [closed]
Python Pandas IF ELSE [closed]

Time:10-01

Would appreciate any help on this, trying to do my first Pandas IF ELSE statement, but I'm struggling with the syntax...

if g['Operator'] == 100151:    
    g['floor']=g['y'].mean() 
elif g['Operator'] == 20137: 
    g['floor']=g['y'].mean() 
elif g['Operator'] == 152: 
    g['floor']=g['y'].mean() 
else: 
    g['floor']=g['y'].mean()/2

Thanks Gav

CodePudding user response:

You can tackle it like this:

import numpy as np
g['floor'] = np.where(g['Operator'].isin([100151, 20137, 152]), g['y'].mean(), g['y'].mean() / 2)

Check the docs for where (basically a vectorized if-else), and isin

  • Related