0

I am using jupyter notebook to access Teradata database.

Assume I have a dataframe

Name  Age
 Sam   5
 Tom   6
 Roy   7

I want to let the whole column "Name" content become the WHERE condition of a sql query.

query = '''select Age
           from xxx
           where Name in (Sam, Tom, Roy)'''
age = pd.read_sql(query,conn)

How to format the column so that the whole column can be insert to the sql statement automatically instead of manually paste the column content?

1 Answer 1

2

Join the Name column and insert into the query using f-string:

query = f'''select Age
           from xxx
           where Name in ({", ".join(df.Name)})'''

print(query)
select Age
           from xxx
           where Name in (Sam, Tom, Roy)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.