itter from some odbc connection to pandas table with out a csv

岁酱吖の 提交于 2019-12-24 15:33:09

问题


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?

It just seems like a waste to have this extra step out to csv and into a DataFrame.


回答1:


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.

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 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.



来源:https://stackoverflow.com/questions/13570178/itter-from-some-odbc-connection-to-pandas-table-with-out-a-csv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!