'The truth value of a is ambiguous when I used Iflese in Python

I am using conditional multiplication within data frame and using following syntax:

if(df_merged1["region_id"]=="EMEA"):
    df_merged1["fcst_gr"] = df_merged1["plan_price_amount"]*(df_merged1["Enr"]-df_merged1["FM_f"])+df_merged1["OA_f"]-df_merged1["TX_f"]
else:
    df_merged1["fcst_gr"] = df_merged1["plan_price_amount"]*(df_merged1["Enr"]-df_merged1["FM_f"])+df_merged1["OA_f"] 

i want tax to be substracted only when region is EMEA. but getting following error

ValueError: The truth value of a {type(self).__name__} is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I think there is some problem in proving the if condition but how to resolve it not getting any idea



Solution 1:[1]

There is no problem here - df_merged1["region_id"] == "EMEA" returns a pd.Series instance populated with boolean values, not a boolean that can be handled using conditional statements. Pandas is reluctant to automatically run a method that would convert a pd.Series instance to a boolean like Series.any() or Series.all(), hence the error.

To achieve what you have meant to do, use df.apply, axis=1 with a lambda expression and a ternary operator. That way you populate a column ["fcst_gr"] based on value in column ["region_id"] for each individual row:

df_merged1["fcst_gr"] = df_merged1.apply(
    lambda row: row["plan_price_amount"] * (row["Enr"] - row["FM_f"])
    + row["OA_f"]
    - row["TX_f"]
    if row["region_id"] == "EMEA"
    else row["plan_price_amount"] * (row["Enr"] - row["FM_f"]) + row["OA_f"],
    axis=1,
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1