2016-07-13 1 views
0

질문이 있습니다. SQL 쿼리에서 열 이름을 pandas 데이터 프레임에 어떻게 추가 할 수 있습니까? 나는 다음 일을하고 있지만 열 = 열은 제 경우에는 작동하지 않습니다.pymsql 및 pandas

import pymssql 
import pandas as pd 


con = pymssql.connect(
    server="MSSQLSERVER", 
    port="1433", 
    user="us", 
    password="pass", 
    database="loc") 

cur = conn.cursor() 

cur.execute("SELECT id from ide") 

data=cur.fetchall() 

pandas=pd.DataFrame(data) 

cur.close() 
con.close() 

그래서 "팬더"를 인쇄하면 헤더가없는 것으로 판명됩니다. 그러면 어떻게받을 수 있습니까?

+0

봐 : 당신이 그런 팬더를 사용

import pyodbc # Parameters server = 'server_name' db = 'db_name' # Create the connection conn = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db + ';Trusted_Connection=yes') 

MSSQL

사용했다 http://pandas.pydata.org/pandas-docs /stable/generated/pandas.read_sql_table.html#pandas.read_sql_table – DeepSpace

+0

안녕하세요, 제 경우에는 SQL 작업을 읽지 마십시오. 다른 제안을하십시오. – HalfPintBoy

+0

의견을 추가했습니다. 작동하는지 알려주세요. – SerialDev

답변

0

먼저 연결을 설정 : read_sql_table``에

df = pandas.read_sql(sql, conn)