Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge Pandas Dataframes based on substring or partial match in another Dataframe

I have two sample data frames:

df1 = pd.DataFrame({'Model': ['0RW52HC5KDD13R', '0RW52HC5KDD13U','JJS42HC5JSSAYR']})

df2 = pd.DataFrame({'Group_Var': ['0RW52HC5K', '0RW52HC5K','JJS42HC5J']})

Using this will result result in an empty dataframe.

df3 = df1.merge(df2, left_on='Model', right_on='Group_Var')

How could I go about using a merge to use a substring / partial match from df2['Group_Var'] in df1['Model']? Perhaps using the str.contains() method as part of the merge?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Just for context my expected output would be something like this:

Group_Var        Model
0RW52HC5K        0RW52HC5KDD13R
0RW52HC5K        0RW52HC5KDD13U
JJS42HC5J        JJS42HC5JSSAYR

>Solution :

Use pd.concat with axis=1:

df3 = pd.concat([df1, df2], axis=1)

Output:

>>> df3
            Model  Group_Var
0  0RW52HC5KDD13R  0RW52HC5K
1  0RW52HC5KDD13U  0RW52HC5K
2  JJS42HC5JSSAYR  JJS42HC5J
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading