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

Question:

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

Asked By: Ashish

||

Answers:

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 pd.Series.any() or pd.Series.all(), hence the error.

To achieve what you have meant to do for reasonably sized dataframes, use pd.DataFrame.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,
)

For bigger dataframes or more complex scenarios, consider more efficient solutions.

Answered By: Vladimir Vilimaitis
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.