Q:

Merge two dataframes using pandas by text similarity

I run a query like below:
select * 
from sd_sms LEFT JOIN categories_phrases 
    on sd_sms.body like  concat('%',categories_phrases.phrase1,'%')
    and sd_sms.body like concat('%',categories_phrases.phrase2,'%')
    and sd_sms.body like concat('%',categories_phrases.phrase3,'%')
    and sd_sms.body like concat('%',categories_phrases.phrase4,'%')

Basically, it will join two tables if one field in table A contains several phrases in table B. But now I need to do this in Python.
Is there any easy way to merge both tables using pandas so it give me same result?
Please advise

A:

This code example works with text data and like condition in the join clause.
from pandasql import *
import pandas as pd

pysqldf = lambda q: sqldf(q, globals())

df1 = pd.DataFrame({"name": ['Antony', 'Mark', 'Jacob'], "age":
                                                         [11,12,13]})
df2 = pd.DataFrame({"name": ['Antony', 'Gill', 'John']})

q = """SELECT * FROM df1 LEFT JOIN df2 ON df1.name LIKE '%' || df2.name || '%'"""

df = pysqldf(q)

This is just a dummy DF with example data but I applied a similar condition to your question.
Hope it could be useful.

