dimanche 19 mai 2019

How to create New Column containing Calculated Values with Conditional Statements in play

Context:

  1. I have a dataframe on football match statistics
  2. This dataframe is missing info on outcome of the match
  3. Hence, I am trying to create a column that lets me know if the match outcome is a "Win" or "Lose"
  4. The dataframe contains goals scored by Home & Away teams
  5. I am trying to populate a new column with "Win" when Home goals is more than Away goals
  6. You can see in my code below that the new column I was trying to create is called "outcome"

Below's working code should give a sense of what I am trying to do, and am doing wrong...

for index, row in match_df.iterrows():
    if match_df.home_team_goal > match_df.away_team_goal:
        match_df.loc[index, "outcome"] = "Win"
    else:
        match_df.loc[index, "outcome"] = "Lose"


Error Message:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Aucun commentaire:

Enregistrer un commentaire