I'm trying to translate a If statement from excel to python.
I know that for excel, If statement we have:
IF(logical_test, [value_if_true], [value_if_false])
For Python I want to replicate the below excel if statement.
AZ2 = IF(BA2<>"",IF(BB2<>"",BB2,"Blank"), IF(BO2<>"", BO2, "Blank"))
Here AZ2,BA2,BB2,BO2 are columns in excel file and <> represents not equal in excel.
I tried using below code using np.where but it does not work? do you know what I am doing wrong?
df['AZ']=np.where(df['BA'].notnull(),
np.where(df['BB'].notnull(),df['BB'],'Blank',
np.where(df['BO'].notnull(),df['BO'],'Blank')))
I get this Error when I tried executing it.
Type Error: where() takes at most 3 arguments (4 given)
Any suggestions?
CodePudding user response:
The issue comes from the bracket missing after the first Blank
and the extra one at the end of the statement.
You can try this one :
df['AZ']=np.where(df['BA'].notnull(),
np.where(df['BB'].notnull(),
df['BB'],
'Blank'),
np.where(df['BO'].notnull(),
df['BO'],
'Blank'))