0

I'm analyzing the international football dataset to get practice on my python and data analysis knowledge it contains a column for home scores and a column for away scores and I want another column to display the winning team name. I want to fill the winner_id column based on the winning criteria. When I write the code as follows it fills it correctly:

results['winner_id'] = results.loc[results['home_score'] > results['away_score'],'home_team']

When I add the next code with the next winning criteria, it deletes all previous values:

results['winner_id'] = results.loc[results['home_score'] > results['away_score'],'home_team']
results['winner_id'] = results.loc[results['home_score'] < results['away_score'],'away_team']

I made these steps before without any problem. Is there any thing I did it wrong for this piece of code

I expecting to find a solution or a suggestion to fix the above problem

  • 1
    Welcome to SO! [Please do not post images of code/data/errors](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors). Could you please also share the code so that the question is a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – medium-dimensional Dec 20 '22 at 12:52
  • 1
    difficult to debug without a reproducible example, please provide text or better a DataFrame constructor – mozway Dec 20 '22 at 12:52
  • Does this answer your question? https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column. Particularly, please look at the solution in the linked post where `np.select` is used. – medium-dimensional Dec 20 '22 at 13:00
  • Each of those lines is creating a new column (as a Series object) and assigning it to the `winner_id` column. You need a method that will combine those results in one operation. See the answers to [Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas](https://stackoverflow.com/q/26886653/1288). – Bill the Lizard Dec 20 '22 at 13:04

1 Answers1

0

You could try:

df['winner_id'] = np.select(
    [
        (df['home_score'] > df['away_score']),
        (df['home_score'] < df['away_score'])
    ],
    [df['home_team'], df['away_team']], default='draw')   
Paul
  • 1,801
  • 1
  • 12
  • 18