[英]How to use Pandas.DataFrame as input of SQL query?
I am trying to use Pandas.DataFrame as the intermediate result dataset between two consequent SQL queries. 我正在尝试使用Pandas.DataFrame作为两个后续SQL查询之间的中间结果数据集。
I imagine it looks like: 我想它看起来像:
import pandas.io.sql as pisql
import pyodbc
SQL_command1 = """
select * from tab_A
"""
result = pisql.read_frame(SQL_command1)
SQL_command2 = """
select *
from ? A
inner join B
on A.id = B.id
"""
pyodbc.cursor.execute(SQL_command2, result)
The SQL_command2
in above code is simply a pseudo code, where ? 上面的代码中的
SQL_command2
只是一个伪代码,其中? takes in the result
as the input and given a alias name as A
. 接受
result
作为输入,并给别名命名为A
This is my first time using Pandas
, so I'm not confident if my idea is feasible or efficient. 这是我第一次使用
Pandas
,所以我不确定我的想法是否可行或有效。 Can anyone enlight me please? 谁能开导我?
Many thanks. 非常感谢。
The pseudo code would look like this 伪代码看起来像这样
import pandas as pd
df_a = pd.read_csv('tab_a.csv') #or read_sql or other read engine
df_b = pd.read_csv('tab_b.csv')
result = pd.merge(left=df_a,
right=df_b,
how='inner',
on='id') #assuming 'id' is in both table
And to select columns of pandas dataframe, it would be something like df_a[['col1','col2','col3']]
要选择熊猫数据
df_a[['col1','col2','col3']]
列,将类似于df_a[['col1','col2','col3']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.