Reputation: 9
I'm just messing around with some football data and I essentially want to add 2 dataframes together on a couple columns.
Here's what I have:
import pandas as pd
d1 = {'PlayerID': [1, 2], 'Name': ['Tyreek', 'Amari'], 'Targets': [15, 16], 'Receptions': [11, 13]}
df1 = pd.DataFrame(data=d1)
d2 = {'PlayerID': [1, 2], 'Name': ['Tyreek', 'Amari'], 'Targets': [4, 5], 'Receptions': [3,3]}
df2 = pd.DataFrame(data=d2)
desired output
[PlayerID|Name|Targets|Receptions]
[ 1 |Tyreek| 19 | 14]
[ 2 |Amari | 21 | 16]
Both dataframes have the same columns but are from different weeks of the season. I've tried merge/concat/add but that just adds weeks separately. I would like to create a season to date dataframe that just adds the latest week. For example Tyreek Hill had 15 targets in week one and 4 targets in week 2, so the season to date dataframe should show 19 targets.
Upvotes: 0
Views: 43
Reputation: 153500
Try:
df1.set_index(['PlayerID', 'Name']).add(df2.set_index(['PlayerID', 'Name'])).reset_index()
Output:
PlayerID Name Targets Receptions
0 1 Tyreek 19 14
1 2 Amari 21 16
Upvotes: 1