Python 使用pyodbc将数据从sqlserver读取到pandas

Python 使用pyodbc将数据从sqlserver读取到pandas,python,sql,sql-server,pandas,Python,Sql,Sql Server,Pandas,我正在尝试将pandas中的SQL server数据作为数据帧导入。我目前拥有以下代码: import pandas as pd import pyodbc # SQL Authentication conn = pyodbc.connect( 'Driver={SQL Server};' 'Server=Servername;' 'Database=DBName;' 'UID=logIn;' 'PWD=Passw

我正在尝试将pandas中的SQL server数据作为数据帧导入。我目前拥有以下代码:

import pandas as pd
import pyodbc

# SQL Authentication
conn = pyodbc.connect(
        'Driver={SQL Server};'
        'Server=Servername;'
        'Database=DBName;'
        'UID=logIn;'
        'PWD=Password;')
    
cursor = conn.cursor()
    
cursor.execute('SELECT * FROM Table')
    
result = cursor.fetchall()
     
cursor.close()
print(result)
这将返回数据,但格式如下:

[(24609189, 'EURTRY.pro', Decimal('5'), Decimal('11'), Decimal('5.0000'), Decimal('105.0000'), Decimal('-2.200000'), Decimal('0.000000'), datetime.datetime(2018, 10, 15, 16, 47, 17, 800000), None)]

我如何才能将其放入数据帧中,以及有没有办法删除数字前面的
十进制

您应该能够直接使用Pandas中的
read_sql()
执行此操作:

import pandas as pd
import pyodbc

# SQL Authentication
conn = pyodbc.connect(
        'Driver={SQL Server};'
        'Server=Servername;'
        'Database=DBName;'
        'UID=logIn;'
        'PWD=Password;') 

sql = 'SELECT * FROM Table'
data = pd.read_sql(sql,conn)

您是否尝试过本机pandas方法
pd.read\u sql(“SELECT*FROM schema.Table”,conn)
非常感谢!不客气。如果答案对您有帮助,请随时接受和/或投票,请参阅