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

标签 python pandas dataframe merge

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

Player| club_id | round  
a     |     16  |   1
b     |     13  |   1
c     |     12  |   1
a     |     16  |   2
...

--------

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
...

我想合并两个数据帧以查看球员是否在主场比赛以及比赛的得分。
最终的数据框可能是这样的:

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
...

我尝试将 home_club_id 更改为 club_id 并与 on =[round,club_id] 合并,但我没有找到合并的方法同时在家和外出

最佳答案

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

首先,我们假设您的框架名为 player_frameround_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+')

然后,我们可以拉出列来分别引用主场和客场数据,重命名以使它们匹配,并标记该行是否是主场比赛。

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')

然后我们可以将两者concat并合并到player_frame中:

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

关于python - 合并在列中迭代的两个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56131746/

相关文章:

python - 组合两个不同的数据框以显示所有可能的迭代

python - 如何删除数据框列中的字符串子串?

python - 将校正列添加到数据框

python - 过滤后在 Pandas 中添加每组出现的列和值

python - 在 Pandas 中向先前的单元格值添加计数

python - 使用列表中的字符串动态创建嵌套的字典

python - NLTK SVM 分类器终止

python - 根据其他列修改数据框的部分列值

python - 如何抓取 youtube 视频以获取比一页更多的视频?

python - 使用 getattr 获取包含在描述符中的方法