8

I have a pandas dataframe that has a column of IDs. I need to run another sql query whose 'WHERE' clause is dictated by all of the IDs in the aforementioned column.

Ex:

df1 = DataFrame({'IDs' : [1,2,3,4,5,6]})

query = """ Select id, SUM(revenue) AS revenue WHERE id IN (***want df1['IDs'] here***) Group by 1"""

df2 = my_database.select_dataframe(query)

2 Answers 2

7

Convert the series to string

str = ','.join([str(x) for x in df1['IDs'].tolist()])

str
'1,2,3,4,5,6'

And, then insert it into the query string -

qry = "Select id, SUM(revenue) AS revenue WHERE id IN (%s) Group by 1" % str

qry
'Select id, SUM(revenue) AS revenue WHERE id IN (1,2,3,4,5,6) Group by 1'
Sign up to request clarification or add additional context in comments.

Comments

3

for this to work for me I had to surround the list items with single quotes.

str = ','.join(["'" + str(x) + "'" for x in df1['IDs'].tolist()])

1 Comment

Then your IDs aren't an integer column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.