4

I have two large data frames which I need to merge row-wise. These two dataframes may have a few overlapping rows.

Example:

data1
    key name               patent
    11  Alphagrep          112344
    12  Citrix             112345

data2
    Sr  name               patents
    11  Alphagrep          112344
    13  Taj                112322

I want to merge these dataframes row wise on [Key,Sr] & [patent, patents]

Which is: If row-data1[key]==row-data2[Sr] & row-data1[patent]==row-data2[patents], Merge or else append.

Result should be:

data1 + data2
    key name               patent
    11  Alphagrep          112344
    12  Citrix             112345
    13  Taj                112322

How should one do this in pandas?

2 Answers 2

5

You can do something like this:

print data1, '\n', '\n', data2, '\n'
data2.columns = data1.columns.tolist()
data3 = pd.concat([data1, data2]).drop_duplicates()
print data3

Output:

   key       name  patent
0   11  Alphagrep  112344
1   12     Citrix  112345 

   Sr       name  patents
0  11  Alphagrep   112344
1  13        Taj   112322 

   key       name  patent
0   11  Alphagrep  112344
1   12     Citrix  112345
1   13        Taj  112322
Sign up to request clarification or add additional context in comments.

3 Comments

This is a nice hack to get around this problem. However, say we have a situation wherein the columns cannot be homogenized ( number of columns not equal), how would we work then? Is there a way that we provide parameters and only those columns be checked
@user248884, could you post reproducible sample data sets and a desired resulting data set? Or even better - open a new question...
Yeah. Create a specific case and post it as a new question. As this solution works for the existing problem you may accept it :)
1

set_index and combine_first

c1 = ['key', 'patent']
c2 = ['Sr', 'patents']

data1.set_index(c1) \
    .combine_first(
        data2.set_index(c2).rename_axis(c1)
    ).reset_index()

   key  patent       name
0   11  112344  Alphagrep
1   12  112345     Citrix
2   13  112322        Taj

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.