Skip to content
Advertisement

how to insert pandas dataframe into IN operator of SQL

I have pandas dataframe with unique number of user:

data_frame = pd.DataFrame({'uniq_num' :['1qw3','2wed','3das','4frr','533ew','612w']})

I want to pass this column to sql query where I use IN operator:

SELECT users FROM database
where users IN ("here I want to pass my dataframe, so it would search in all rows of my dataframe")

I have tried doing this

    data_frame = ','.join([str(x) for x in data_frame.iloc[:, 0].tolist()])

which would retrun whith this '1qw3,2wed,3das,4frr,533ew,612w'

and then something like WHERE users in STRING_SPLIT(data_frame, ',') but this one is obviousely doesnt work…

Advertisement

Answer

You can convert the list into a tuple, this will give you the correct format

import pandas as pd
data_frame = pd.DataFrame({'uniq_num' :['1qw3','2wed','3das','4frr','533ew','612w']})

in_statement = tuple(data_frame.iloc[:, 0].tolist())
sql = f"""SELECT users FROM database
where users IN {in_statement}"""

output sql variable:

SELECT users FROM database
where users IN ('1qw3', '2wed', '3das', '4frr', '533ew', '612w')
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement