Python 合并在列中迭代的两个数据帧

Python 合并在列中迭代的两个数据帧,python,pandas,dataframe,merge,Python,Pandas,Dataframe,Merge,我有两个数据框,一个是球员,有他们的俱乐部ID和回合,另一个是比赛,有分数和回合 Player| club_id | round a | 16 | 1 b | 13 | 1 c | 12 | 1 a | 16 | 2 ... 球员|俱乐部| id |轮 a | 16 | 1 b | 13 | 1 c | 12 | 1 a | 16 | 2 ... ------- 主场俱乐部id客场俱乐部id主场俱乐部得分

我有两个数据框,一个是球员,有他们的俱乐部ID和回合,另一个是比赛,有分数和回合

Player| club_id | round a | 16 | 1 b | 13 | 1 c | 12 | 1 a | 16 | 2 ... 球员|俱乐部| id |轮 a | 16 | 1 b | 13 | 1 c | 12 | 1 a | 16 | 2 ... ------- 主场俱乐部id客场俱乐部id主场俱乐部得分客场俱乐部得分回合 16 | 13 | 1 |2 |1 15 | 1 | 4 |0 |1 12 | 2 | 1 |1 |1 12 | 16 | 2 |2 |2 ... 我想合并两个数据帧,以查看球员是否在主场比赛,以及比赛的比分。
最终的数据帧可以是这样的:

Player|club_id|round|home|score|opponent_score a |16 |1 | yes|1 | 2 b |13 |1 | no |2 | 1 a |16 |2 | no |2 | 2 ... 球员|俱乐部| id |回合|主场|得分|对手|得分 a | 16 | 1 |是| 1 | 2 b | 13 | 1 | no | 2 | 1 a | 16 | 2 | no | 2 | 2 ...
我试图将
home\u club\u id
更改为
club\u id
并与
on=[round,club\u id]
合并,但我没有找到一种方法同时合并主客场

以获得所需的最终帧,您可以改为重新排列数据

首先,假设您的帧被称为
player\u-frame
round\u-frame

from io import StringIO

import pandas as pd

player_data = StringIO('''Player club_id  round  
a          16     1
b          13     1
c          12     1
a          16     2''')
player_frame = pd.read_csv(player_data, sep='\s+')

round_data = StringIO('''home_club_id away_club_id home_club_score away_club_score round  
16               13          1           2               1
15               1           4           0               1
12               2           1           1               1
12               16          2           2               2''')
round_frame = pd.read_csv(round_data, sep='\s+')
final_values = pd.concat([home_values, away_values], ignore_index=True).merge(player_frame)
然后,我们可以拉出列来分别引用主数据和客场数据,重命名以使它们匹配,并标记行是否为主匹配

home_values = round_frame[['home_club_id', 'home_club_score', 'away_club_score', 'round']]\
                         .rename({'home_club_id': 'club_id', 
                                  'home_club_score': 'score', 
                                  'away_club_score': 'opponent_score'},
                                 axis=1)\
                         .assign(home='yes')

away_values = round_frame[['away_club_id', 'away_club_score', 'home_club_score', 'round']]\
                         .rename({'away_club_id': 'club_id', 
                                  'home_club_score': 'opponent_score', 
                                  'away_club_score': 'score'},
                                 axis=1)\
                         .assign(home='no')
然后我们可以将两者合并到播放器框架中:

from io import StringIO

import pandas as pd

player_data = StringIO('''Player club_id  round  
a          16     1
b          13     1
c          12     1
a          16     2''')
player_frame = pd.read_csv(player_data, sep='\s+')

round_data = StringIO('''home_club_id away_club_id home_club_score away_club_score round  
16               13          1           2               1
15               1           4           0               1
12               2           1           1               1
12               16          2           2               2''')
round_frame = pd.read_csv(round_data, sep='\s+')
final_values = pd.concat([home_values, away_values], ignore_index=True).merge(player_frame)
这给了我们:

   club_id  score  opponent_score  round home Player
0       16      1               2      1  yes      a
1       12      1               1      1  yes      c
2       13      2               1      1   no      b
3       16      2               2      2   no      a