简体   繁体   中英

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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM