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

问题描述 投票:0回答:2

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

玩家|俱乐部ID |圆形的
一个 | 16 | 16 1
乙| 13 | 1
c | 12 | 12 1
一个 | 16 | 16 2
...

------

home_club_id| away_club_id |home_club_score|客场俱乐部得分|圆形的
16 | 16 13 | 1 |2 |1
15 | 15 1 | 4 |0 |1
12 | 12 2 | 1 |1 |1
12 | 12 16 | 16 2 |2 |2
...

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

玩家|club_id|回合|主场|比分|对手比分
一个 |16 |1 |是|1 | 2
b |13 |1 |没有 |2 | 1
一个 |16 |2 |没有 |2 | 2
...

我尝试将

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

python pandas dataframe merge
2个回答
2
投票

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

首先,我们假设您的框架名为

player_frame
round_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

0
投票
duckdb:

df1.sql.set_alias("tb1").join(df2.sql.set_alias("tb2"),condition="tb1.round=tb2.round and (tb1.club_id=tb2.home_club_id or tb1.club_id=tb2.away_club_id)",how="left").select("player,club_id,tb1.round,case when tb1.club_id=tb2.home_club_id then 'yes' else 'no' end home,case when tb1.club_id=tb2.home_club_id then home_club_score else away_club_score end score,case when tb1.club_id=tb2.away_club_id then away_club_score else home_club_score end opponent_score")

┌─────────┬─────────┬───────┬─────────┬───────┬────────────────┐
│ Player  │ club_id │ round │  home   │ score │ opponent_score │
│ varchar │  int64  │ int64 │ varchar │ int64 │     int64      │
├─────────┼─────────┼───────┼─────────┼───────┼────────────────┤
│ a       │      16 │     1 │ yes     │     1 │              1 │
│ b       │      13 │     1 │ no      │     2 │              2 │
│ c       │      12 │     1 │ yes     │     1 │              1 │
│ a       │      16 │     2 │ no      │     2 │              2 │
└─────────┴─────────┴───────┴─────────┴───────┴────────────────┘
© www.soinside.com 2019 - 2024. All rights reserved.