Search code examples
pythonvectorternary

Python ternary operation on vectors


Could someone help me with the proper format of a python ternary operation on a vector? I have two dataframes temperature: df_today and df_yesterday. I am trying to calculate a new column for df_today to determine whether the temperature is warmer than yesterday:

df["warmer_than_yesterday"] = 'yes, warmer' if df["temp_celsius"] > df_yesterday["temp_celsius"] and df["temp_celsius"] > 10 else 'nah, not warmer'

However, I keep getting the error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Does anyone know what I might be doing wrong?

Thanks in advance!


Solution

  • First, you can combine your if conditions into one, using np.maximum (for conciseness). Should also be more performant.

    m = df["temp_celsius"] > np.maximum(10, df_yesterday["temp_celsius"]) 
    

    Now, pass this mask to np.where,

    df["warmer_than_yesterday"] = np.where(m, 'yes', 'no')
    

    Or, to loc to set slices:

    df["warmer_than_yesterday"] = 'no'
    df.loc[m, "warmer_than_yesterday"] = 'yes'