Home > Software engineering >  Translate Excel if condition from Excel to Python
Translate Excel if condition from Excel to Python

Time:10-27

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 IFERROR statement we have:

IFERROR(value, value_if_error)

OR function:

The OR function returns TRUE if any of its arguments evaluate to TRUE, and returns FALSE if all of its arguments evaluate to FALSE.

For Python I want to replicate the below two excel if statement.

=IF( OR(IFERROR(AM2 0=0, AM2=""), DK2<>" "), " ", AM2)

Here AM2,DK2 are columns in excel file and <> represents not equal in excel.

Can anyone help me translate this into Python it was quiet confusing for me.

CodePudding user response:

Something like this (if we talk about pandas DataFrame):

df['whatever'] = np.where(
    (df['AM'] == 0) | (df['AM'].isna()) | (df['AM'] == " ") | (df['DK'] != ' ') | (df['AM'] == ""),
    "ZZZ",
    df['AM']
)

I used letters as column names since no column names were provided.

  • Related