I have two pandas dataframes df1 and df2. I need to create a new column in df1 by searching df2['B'] to see if df1['A'] is a substring of df2['B'] or vice versa. If there is a match return the value of df2['A'] for the new column in df1['B'].
Below are sample dataframes
df1
A B
8GSHDK1 ?
SDFAGHJFDJ GSHJGGFV
678HJDGGH
576GHJHJJKHJJH
YRYWEUIYWRE
df2
A B
1 GSHJGGFV
2 XXXYYYYY
3 8GSHDK1 TO BE DEL
Merge wouldn't work in this case since df1['A'] contains substring of df2['B'] Or df2['B'] contains substring of df1['A'].
I have tried below but it runs for 7 to 8 hours. df1 has 25k records and df2 has 720k records
df1['B']=df1['A'].apply(lambda x: df2[df2['B'].str.contains(x) | df2['B'].apply(lambda y : y in x)]['B'].any())
Any help will be very much appreciated. Expected output :
df1
A B
8GSHDK1 8GSHDK1 TO BE DEL
SDFAGHJFDJ GSHJGGFV GSHJGGFV
678HJDGGH None
576GHJHJJKHJJH None
YRYWEUIYWRE None