简体   繁体   English

在Pandas DataFrame中,如何合并/合并两个具有左表中所有行的DataFrame,并从右DataFrame中重复值

[英]In Pandas DataFrame how to merge/join two DataFrame that has all row from left table and repeat values from right DataFrame

If df1 looks like: 如果df1看起来像:

Build_ID, Request_ID, Group_ID, Average
185, 100, G1, 200
186, 100, G1, 201
185, 102, G1, 203
186, 102, G1, 205
185, 200, G3, 200
186, 200, G3, 201
185, 202, G3, 203
186, 202, G3, 205

and df2 looks like: 和df2看起来像:

Build_ID, Group_ID, Group_Average
185, G1, 300
186, G1, 301
185, G3, 401
186, G3, 402

and final result should look like: 最终结果应如下所示:

 Build_ID, Request_ID, Group_ID, Average, Group_Average
    185, 100, G1, 200, 300
    186, 100, G1, 201, 301
    185, 102, G1, 203, 300
    186, 102, G1, 205, 301
    185, 200, G3, 200, 401
    186, 200, G3, 201, 402
    185, 202, G3, 203, 401
    186, 202, G3, 205, 402

Which basically has all rows from df1 and Group_Average from df2 is repeated for each Group_ID and Build_ID. 对于每个Group_ID和Build_ID,基本上都重复了df1和df2的Group_Average的所有行。 I tried merge and join using different joint but can't get the result I am looking for. 我尝试使用不同的关节进行合并和合并,但无法获得所需的结果。 Thanks 谢谢

Is that what you want? 那是你要的吗?

In [60]: df1.merge(df2, on=['Build_ID','Group_ID'])
Out[60]:
   Build_ID  Request_ID Group_ID  Average  Group_Average
0       185         100       G1      200            300
1       185         102       G1      203            300
2       186         100       G1      201            301
3       186         102       G1      205            301
4       185         200       G3      200            401
5       185         202       G3      203            401
6       186         200       G3      201            402
7       186         202       G3      205            402

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

相关问题 与 Pandas 中左侧数据帧的覆盖值合并 - Merge with overwrite values from left dataframe in pandas 如果在右侧数据框中找不到左侧数据框中的值,如何使用合并在熊猫中执行VLOOKUP? - How do I perform a VLOOKUP in pandas using merge where NaN is returned if values from left dataframe not found in right dataframe? pandas dataframe是从右到左? - pandas dataframe is from the right to the left? 将右 dataframe 合并到左 dataframe,优先选择右 dataframe 的值并保留新行 - Merge right dataframe into left dataframe, preferring values from right dataframe and keeping new rows 如何在 dataframe 中从右到左填充 0 值? - How to fill 0 values in dataframe from right to left? 如何合并来自同一行和列索引/值的两个熊猫数据框的值? - How can I merge the values from two pandas dataframe which as same row and column indexes/value? 如何从两个熊猫数据框中加入像元值? - How can I join the cell values from two pandas dataframe? 如何对两个 dataframe 中的一行求和,该行在 Pandas Dataframe 中具有相同的值 - How to sum a row from two dataframe that has the row with certain same value in Pandas Dataframe 从两列合并 pandas dataframe - merge pandas dataframe from two columns Python_Executing 特定行的所有值来自 Pandas dataframe - Python_Executing specific row by all values from Pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM