简体   繁体   English

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

[英]How to merge two dataframes by using a common column?

I have two dataframes namely high_scores1 and high_scores2 like this: 我有两个数据帧,如high_scores1和high_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

Now i need to merge these two using the column match_id such that the new dataframe looks something like this: 现在我需要使用列match_id合并这两个,以便新数据框看起来像这样:

match_id  inn   batting_team    bowling_team    runs1   inn2  runs2
1          1    KKR                 RCB         222     2     82
2          1    CSK                 KXIP        240     2     207
3          1    RR                  DD          129     2     132
4          1    MI                  RCB         165     2     166

I tried the following code: 我尝试了以下代码:

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

But it did not work. 但它没有用。 How do i merge the dataframes? 我如何合并数据帧?

你需要

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

Just merge on a subset of the second DataFrame's columns with 只需合并第二个DataFrame列的子集

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

Demo 演示

>>> high_scores1.merge(high_scores2[['match_id','inn2', 'runs2']], on='match_id')
  batting_team bowling_team  inn1  match_id  runs1  inn2  runs2
0          KKR          RCB     1         1    222     2     82
1          CSK         KXIP     1         2    240     2    207
2           RR           DD     1         3    129     2    132
3           MI          RCB     1         4    165     2    166

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于公共列但值重复来合并两个数据框? - How to merge two dataframes based on a common column but duplicated values? 将两个数据框合并到一个公共列 - Merge two dataframes one common column 在python中使用公共列和字符串条件合并两个熊猫数据帧 - Merge two pandas dataframes in python with a common column and with condition on string 如何基于使用一列的 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 合并两个数据帧 - How to merge two dataframes based on using the substring of one column 如何通过公共列(ID)将两个分组熊猫数据帧合并在一起? - How to merge two grouped-by Pandas Dataframes by a common column(ID) together? 如何基于一个公共列但不同的内容合并/扩展两个 python pandas 数据帧? - How do I merge/expand two python pandas dataframes, based on one common column but different content? 如何基于公共列合并两个不同行长度的数据帧 - How can I merge two dataframes of different row lengths based on a common column 如何在集合列上合并两个熊猫数据框 - How to merge two pandas dataframes on column of sets 如何将 3 个数据框的列与 python 中的两个条件合并 - How to merge 3 dataframes' column with two criteria in python 如何在同一列上合并数据框,同时对齐其他列 - How to merge dataframes on one column while aligning the other columns in common
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM