[英]Python variables in Jupyter SQL cells
I have a dataframe that needs to be joined with the result set from a query.我有一个需要与查询结果集连接的数据框。 The query uses a column from a dataframe to filter the data in the database.
该查询使用数据框中的列来过滤数据库中的数据。
data_list = list(df['needed_column'])
I would like to use the variable in an sql query executed in a Jupyter sql cell.我想在 Jupyter sql 单元中执行的 sql 查询中使用该变量。
%%sql
SELECT
column_1,
column_2,
column_3
FROM my_database.my_table
WHERE
column_1 IN data_list
Is there anyway that this can be done?无论如何,这可以做到吗?
An workaround would be to execute the query inline as a variable.一种解决方法是将查询内联作为变量执行。
data_list = str(list(df['needed_column']).replace('[', '(').replace(']', ')')
query_string = f"""
SELECT
column_1,
column_2,
column_3
FROM my_database.my_table
WHERE
column_1 IN {data_list}
"""
result_set = %sql $query_string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.