jeudi 30 septembre 2021

Creating an if statement to compare column values in a dataframe and replace it with another one?

I currently have a dataframe of football results that goes something like this:

year home_team away_team home_score away_score winner penalty_winner
1872 Scotland England 0 0 Draw NaN
1956 England France 1 1 Draw NaN
1976 France England 4 0 France NaN
1999 Germany Portugal 1 1 Draw Germany
2021 England Italy 0 0 Draw Italy

What I want to do is replace the "Draw" values in the "winner" column with the values in the "penalty_winner" column only if the values are not "NaN".

I tried something like this:

if merged_international_results_df.loc[merged_international_results_df['winner'] == 'Draw'] and merged_international_results_df.loc[merged_international_results_df['penalty_winner'].isnull() == False]:
    merged_international_results_df['winner'] = merged_international_results_df['penalty_winner']

The output I get is

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

How can I correct this?

Aucun commentaire:

Enregistrer un commentaire