Search code examples
pandasjoinmerge

how to combine tw0 dataset with matching index?


i want to combine two data sets with unmatching row entries. the two sets are aligned by matching numbered index. i want to see the left data set (dataset number 1) to retain all data but append/concatenate/join/merge the string values from Dataset number 2. i also want to return only the 2 columns of Dataset number 1. I'm sure there are many methods that might work. i'm looking to understand a method that does it direct as i have many similar cases. data-types may vary.

df1:

       Item Name  Item Size
0     first12345         10
1    second12345         10
2     third12345         10
68   fourth12345         20
88    fifth12345         20
90    sixth12345         10
91  seventh12345         10
92   eighth12345         20

df2:

   Item Name additional
68               789ten
88               789ten
92               789ten

This is how i want things to look:

            Item Name  Item Size
0          first12345         10
1         second12345         10
2          third12345         10
68  fourth12345789ten         20
88   fifth12345789ten         20
90         sixth12345         10
91       seventh12345         10
92  eighth12345789ten         20

Solution

  • Assuming df1/df2, you could get the index intersection and add in place:

    df1.loc[df1.index.intersection(df2.index),
            'Item Name'] += df2['Item Name additional']
    

    Alternatively, using reindex:

    df1['Item Name'] += df2['Item Name additional'].reindex(df1.index, fill_value='')
    

    Or add:

    df1['Item Name'] = df1['Item Name'].add(df2['Item Name additional'],
                                            fill_value='')
    

    Updated df1:

                Item Name  Item Size
    0          first12345         10
    1         second12345         10
    2          third12345         10
    68  fourth12345789ten         20
    88   fifth12345789ten         20
    90         sixth12345         10
    91       seventh12345         10
    92  eighth12345789ten         20