Compare datasets in sql query with pandas

Question:

Cannot see online and therefore not sure if this is possible? Can I make a query to sql that will compare columns in sql to existing pandas dataframe ?

EDIT: adding more of sample code

df_local = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
result = pd.read_sql(
f""" 
SELECT * FROM sql_dataset 
  WHERE sql_dataset.col1 =df.col1 
  AND
  sql_dataset.col2 =df.col2
""" )
Asked By: Zhenia

||

Answers:

Try this:

df_local = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
col1_list = df_local['col1'].values.tolist()
q_col1 = ', '.join([str(row) for row in col1_list])

col2_list = df_local['col2'].values.tolist()
q_col2 = ', '.join([str(row) for row in col2_list])

concat_list = ["'"+str(c1) + ' ' + str(c2)+"'" for c1, c2 in zip(col1_list, col2_list)]
q_concat = ', '.join([str(row) for row in concat_list])


q = "SELECT * FROM sql_dataset WHERE CONCAT(sql_dataset.col1, ' ', sql_dataset.col2) in (" + q_concat + ")"

result = pd.read_sql(q)
Answered By: Oneal R
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.