Home > database >  Python How to loop through each row and see if condition is met between two columns
Python How to loop through each row and see if condition is met between two columns

Time:05-25

I have 3 columns. I want to update the third column, status, if the first column, a is greater than or equal to column b 0.50. I've tried this following code but cannot get it to work:

if df['a'] >= (df['b']   0.50):
    df['status'] = 1

status is already populated with zeros.

how would i go about this?

CodePudding user response:

You can try with np.where

df['status'] = np.where(df['a'] >= (df['b']   0.50),1,0)

Or just

df['status'] = (df['a'] >= (df['b']   0.50)).astype(int)

Or loc

df.loc[(df['a'] >= (df['b']   0.50)),'status'] = 1

CodePudding user response:

You can use lambda function :

def myFunc(x): 
    if(x[0]>=x[1] 0.50) :
        return 1
    else: return x[2]

df['status'] = df.apply(lambda x:myFunc(x),axis=1)
  • Related