Querying from Microsoft SQL to a Pandas Dataframe

∥☆過路亽.° 提交于 2019-12-11 09:26:09

问题


I am trying to write a program in Python3 that will run a query on a table in Microsoft SQL and put the results into a Pandas DataFrame.

My first try of this was the below code, but for some reason I don't understand the columns do not appear in the order I ran them in the query and the order they appear in and the labels they are given as a result change, stuffing up the rest of my program:

 import pandas as pd, pyodbc    

    result_port_mapl = []

    # Use pyodbc to connect to SQL Database
    con_string = 'DRIVER={SQL Server};SERVER='+ <server> +';DATABASE=' + 
<database>


     cnxn = pyodbc.connect(con_string)
    cursor = cnxn.cursor()

    # Run SQL Query
    cursor.execute("""
                   SELECT <field1>, <field2>, <field3>
                   FROM result
                   """)

    # Put data into a list
    for row in cursor.fetchall():
        temp_list = [row[2], row[1], row[0]]
        result_port_mapl.append(temp_list)

    # Make list of results into dataframe with column names
    ## FOR SOME REASON HERE row[1] AND row[0] DO NOT CONSISTENTLY APPEAR IN THE 
    ## SAME ORDER AND SO THEY ARE MISLABELLED
    result_port_map = pd.DataFrame(result_port_mapl, columns={'<field1>', '<field2>', '<field3>'})

I have also tried the following code

    import pandas as pd, pyodbc

    # Use pyodbc to connect to SQL Database
    con_string = 'DRIVER={SQL Server};SERVER='+ <server> +';DATABASE=' + <database>
    cnxn = pyodbc.connect(con_string)
    cursor = cnxn.cursor()

    # Run SQL Query
    cursor.execute("""
                   SELECT <field1>, <field2>, <field3>
                   FROM result
                   """)

    # Put data into DataFrame
    # This becomes one column with a list in it with the three columns 
    # divided by a comma
    result_port_map = pd.DataFrame(cursor.fetchall())

    # Get column headers
    # This gives the error "AttributeError: 'pyodbc.Cursor' object has no 
    # attribute 'keys'"
    result_port_map.columns = cursor.keys()

If anyone could suggest why either of those errors are happening or provide a more efficient way to do it, it would be greatly appreciated.

Thanks


回答1:


If you just use read_sql? Like:

import pandas as pd, pyodbc    
con_string = 'DRIVER={SQL Server};SERVER='+ <server> +';DATABASE=' + <database>
cnxn = pyodbc.connect(con_string)
query = """
  SELECT <field1>, <field2>, <field3>
  FROM result
"""
result_port_map = pd.read_sql(query, cnxn)
result_port_map.columns.tolist()


来源:https://stackoverflow.com/questions/49247884/querying-from-microsoft-sql-to-a-pandas-dataframe

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