如何使用公共列合并两个数据帧?

2024-09-30 08:30:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据帧,即high\u scores1和high\u scores2,如下所示:

match_id    inn1    batting_team    bowling_team    runs1
1              1    KKR                 RCB         222
2              1    CSK                 KXIP        240
3              1    RR                  DD          129
4              1    MI                  RCB         165


match_id    inn2    batting_team    bowling_team    runs2
    1         2     RCB              KKR            82
    2         2     KXIP             CSK            207
    3         2     DD               RR             132
    4         2     RCB              MI             166

现在,我需要使用列match_id合并这两个数据帧,以便新的数据帧如下所示:

^{pr2}$

我尝试了以下代码:

high_scores1[['match_id','inn1','batting_team','bowling_team','runs1']].merge(high_scores2, left_on = 'match_id', right_on = 'match_id', how = 'left')

但它没有起作用。如何合并数据帧?在


Tags: 数据idmatchteamhighbowlingscores1scores2
2条回答

只需使用

high_scores1.merge(high_scores2[['match_id','inn2', 'runs2']], on='match_id')

演示

^{pr2}$

你需要

scores = high_scores1.merge(high_scores2[['match_id', 'inn2', 'runs2']], on = 'match_id')

相关问题 更多 >

    热门问题