从一些 odbc 连接到没有 csv 的 Pandas 表 [英] itter from some odbc connection to pandas table with out a csv
问题描述
我在 Pandas DataFrame 中有一些基本信息.我需要将它与一些我可以通过 pyodbc 连接访问的参考表连接起来.有没有办法将 sql 结果集放入 Pandas DataFrame 而不先将结果集写入 csv?
I have some base info in a pandas DataFrame. I need to join it with some reference tables that I have access via a pyodbc connection. Is there any way to get the sql result set into a pandas DataFrame without writing the result set out to a csv first?
将这个额外的步骤导出到 csv 和 DataFrame 似乎是一种浪费.
It just seems like a waste to have this extra step out to csv and into a DataFrame.
推荐答案
我已经让 pyodbc
与我的 SQL Server
实例一起工作,然后,在一些帮助下这个线程,我得到了sql返回来加载数据帧.
I've gotten pyodbc
to work with my SQL Server
instance, then, with some help from this thread, I got the sql return to load a dataframe.
我已经建立了一个 pyodbc
连接,然后调用了它.
I already setup a pyodbc
connection, then made a call to it.
import pyodbc
import pandas.io.sql as psql
cnxn = pyodbc.connect(your_connection_info)
cursor = cnxn.cursor()
sql = ("""SELECT * FROM Source""")
df = psql.frame_query(sql, cnxn)
cnxn.close()
df
现在应该返回您的数据帧.对我来说最困难的部分是启动并运行 pyodbc
- 我不得不使用 freetds
并且需要大量的反复试验才能让它工作.
df
should return your dataframe now. The hardest part for me was getting pyodbc
up and running - I had to use freetds
and it took a lot of trial and error to get it work.
这篇关于从一些 odbc 连接到没有 csv 的 Pandas 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!