mercredi 4 décembre 2019

new column in pandas dataframe failed to get the expected values basis if conditions on multiple columns

I have a pandas dataframe that has data as in the below table:

Negative  Positive  Neutral
True      False     False
True      False     False
False     False     True
False     True      False
True      False     False
False     True      False
True      False     False
True      False     False

What I am doing is to create a new column ("Overall") and depending upon conditions that if the row value for column "Positive" is True, Overall column gets the value "Positive", if Column "Negative" is True, then Overall will take "Negative" otherwise "Neutral" values:

def flag_df(df):
    if (df["Negative"] == "True") and (df["Positive"] == "False") and (df["Neutral"] == "False"):
        return "Negative"
    elif (df["Negative"] == "False") and (df["Positive"] == "True") and (df["Neutral"] == "False"):
        return "Positive"
    else :
        return "Neutral"

fdf['Overall'] = fdf.apply(flag_df, axis = 1)

but unfortunately, I don't know what I did wrong, all the observations in "Overall" column is coming out to be "Neutral":

Negative    Positive    Neutral     Overall
True           False     False      Neutral
True           False     False      Neutral
False          False     True       Neutral
False          True      False      Neutral
True           False     False      Neutral
False          True      False      Neutral
True           False     False      Neutral
True           False     False      Neutral

Can someone please let me know where I did wrong?

Aucun commentaire:

Enregistrer un commentaire