I have two data frames as below,
listCode= ['A1','A2','A3','A4','A5','A6']
dfLookup = pd.DataFrame({'ID':listCode})
data = [['Chicago', 'B1'], ['Madsion', 'A1'], ['NY', 'A4']]
dftest = pd.DataFrame(data, columns=['City', 'Code', ])
dftest['Found'] =''
dftest
City Code Found
Chicago B1
Madsion A1
NY A4
Expected Result:
City Code Found
Chicago B1 0
Madsion A1 1
NY A4 1
I am trying to join the data frames using the below, but not sure how to use the case statements.
import pandasql as pdsql
import sqldf
sQuery = """
SELECT dftest.City, dftest.Code, dftest.Found
FROM dftest
LEFT JOIN dfLookup
ON dftest.Code= dfLookup.ID"""
sqlResult = sqldf.run(sQuery )
Thanks for your help.
Another possible solution (there is no need to use
pandasql):In case you really want to use
pandasql:Output: