0

Pandas Merging 101 I have checked for answers to my question below already and I didn't find a solution so far.

Suppose we have a DataFrame:

    date   team  gfr  gar  opponent
0   09-22    A    4    1      B
1   05-22    B    1    1      Y
2   09-22    B    2    2      A
3   08-21    B    0    1      D
4   08-21    D    7    3      X
5   05-22    Y    2    2      B

Desired result:

    date   team  gfr  gar  opponent  gfr_opponent  gar_opponent
0   09-22    A    4    1      B          2              2
1   05-22    B    1    1      Y          0              1
2   09-22    B    2    2      A          4              1
3   08-21    B    0    1      D          7              3
4   08-21    D    7    3      B          0              1
5   05-22    Y    0    1      B          1              1


Basically, in a big DataFrame, the goal for row 0 for example is to find what the values gfr and gar for the opponent are, when it is under the 'team' column and assign them to gfr_opponent and gar_opponent, based on the same date. And then do that for every single team and opponent in my DataFrame.

So gfr_opponent = gfr of the same team on the same date.

I am not even sure if my explanation is understandable, but I am doing my best.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Miserry
  • 1
  • 2

0 Answers0