Skip to content
Advertisement

Join two data frames by searching & matching exactly same strings

I have two python dataframes: one of them have a column has a row ‘AC-2’ another data frame includes columns which has a string row ‘AC-20,AC8,AC-15’

JavaScript

First dataset:

JavaScript

second dataset:

JavaScript

I found:

JavaScript

above there is a matching between AC-2 for dataset1 and AC-20 in string for dataset 2 but they are different thing in my dataset. my desired output:

JavaScript

I tried to merge data frames with searching and matching BUT my code merged AC-2 to AC-20. I don’t want to do that. Are there any searching and matching way to merge my data frames exactly what I want. I don’t want to merge AC-20 and AC-2. It should merge AC-2 and AC-2 with exactly same thing.

Thank you for your contributions!

Advertisement

Answer

A simple way is to split and explode the Column2 of df2 to get one row per full word and perform a simple left merge.

JavaScript

output:

JavaScript
alternative

NB. I am producing above a slightly different output to keep all columns. If really you want your output, simply drop “Column2” in df1 and rename “Column1” into “Column2” in df2

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement