I have a question about the following data on a multiple-day match where people collected points on different days of the match. I want to sum the points gotten by one person (with a unique key) on that person's first, second, and third day and I have two data frames for this (see frames A and B). The problem is that the code I wrote to obtain the result, takes a long time and is very inefficient. I can't seem to figure out how to apply/map works in the case where two values have to be matched (not only person key but also the day). Can anybody help me out with this? I am using Pandas in Python.
Example:
Dataframe A
Person key | Start day | Day | Points |
---|---|---|---|
123 | 10 | 10 | 5 |
123 | 10 | 11 | 1 |
888 | 1 | 1 | 6 |
888 | 1 | 2 | 2 |
888 | 1 | 3 | 4 |
Dataframe B
Person key | Day 1 | Day 2 | Day 3 | points day 1 | points day 2 | points day 3 |
---|---|---|---|---|---|---|
123 | 10 | 11 | 12 | 0 | 0 | 0 |
888 | 1 | 2 | 3 | 0 | 0 | 0 |
What I would like to get:
Person key | Day 1 | Day 2 | Day 3 | points day 1 | points day 2 | points day 3 |
---|---|---|---|---|---|---|
123 | 10 | 11 | 12 | 5 | 1 | 0 |
888 | 1 | 2 | 3 | 6 | 2 | 4 |
The code I tried:
for i in range (0,len(dfB)):
for t in range(0,len(dfA)):
if dfB['Day 1'].iloc[i] == dfA['Day'].iloc[t] and dfB['Person key'].iloc[i] == dfA['Person key'].iloc[t]:
dfB['points day 1'].iloc[i] = dfA['Points'].iloc[t]
continue
if dfB['Day 2'].iloc[i] == dfA['Day'].iloc[t] and dfB['Person key'].iloc[i] == dfA['Person key'].iloc[t]:
dfB['points day 2'].iloc[i] = dfA['Points'].iloc[t]
continue
if dfB['Day 3'].iloc[i] == dfA['Day'].iloc[t] and dfB['Person key'].iloc[i] == dfA['Person key'].iloc[t]:
dfB['points day 3'].iloc[i] = dfA['Points'].iloc[t]
continue