user12495822
user12495822

Reputation:

How to concatenate two different dataframes and replace value in column from value in another column?

my question is above. But my problem is a bit complex for me. I have two dataframes like for example Arsenal

#     Date       | HomeTeam | AwayTeam  | PolarityScoreHome | PolarityScoreAway
# 0   2020-10-25 | Leicester| Arsenal   | 0.00              | 0.14
# ...
# 8   2021-04-03 | Arsenal  | Liverpool | 0.13              | 0.00

and for Liverpool dataframe:

#     Date       | HomeTeam  | AwayTeam  | PolarityScoreHome | PolarityScoreAway
# 0   2020-10-25 | Liverpool | Chelsea   | 0.08              | 0.00
# 1   2021-04-03 | Arsenal   | Liverpool | 0.00              | 0.11

How you can see both dataframe have the same row (Index 8 and 1) and I have just replace the value in df Arsenal in column PolarityScoreAway with the value in df Liverpool in column PolarityScoreAway and just to concatenate it.

In other words the value 0.00 in data_arsenal['PolarityScoreAway'] would be replaced by the value 0.11 in data_liverpool['PolarityScoreAway']

So my output would be like :

#     Date       | HomeTeam | AwayTeam  | PolarityScoreHome | PolarityScoreAway
# 0   2020-10-25 | Liverpool| Chelsea   | 0.08              | 0.04
# ...
# 7   2020-10-25 | Leicester| Arsenal   | 0.10              | 0.14
# 8   2021-04-03 | Arsenal  | Liverpool | 0.13              | 0.11

Upvotes: 0

Views: 56

Answers (1)

jezrael
jezrael

Reputation: 862901

If possible simplify solution by join together and aggregate sum for replace 0.00 by matched values use:

df = pd.concat([df1, df2]).groupby(['Date','HomeTeam','AwayTeam'], as_index=False).sum()

Upvotes: 1

Related Questions