Sr. Schneider
Sr. Schneider

Reputation: 777

Merge on substring

I have two dataframes:

df1 with columns A and B and df2 with columns C and D.

I want to merge df1 and df2 under the condition that A contains D.

df1 = [['Hello world',10],['bla bla',12],['okey',13]]
df2 = [['rld',100]]

Result

df_1_2 = [['rld',100,10]]

So the result contains all columns of df1 and df2 (except column A of df1).

Upvotes: 1

Views: 41

Answers (1)

BENY
BENY

Reputation: 323316

We can do findall find the substring then merge

df1['new'] = df1[0].str.findall('|'.join(df2[0].tolist())).str[0]
out = df1.merge(df2,left_on='new',right_on=0)
           0_x  1_x  new  0_y  1_y
0  Hello world   10  rld  rld  100

Upvotes: 1

Related Questions