0

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.

2
  • 2
    Please provide your data frames/code as text and not as images Commented Sep 21, 2021 at 21:18
  • 1
    in addition, could you add the desired result as a text table? Commented Sep 21, 2021 at 21:26

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.