我在mssql中有一个带有数据列的表。我可以成功连接并提取数据,但是当我提取数据时,它将被放入数据框的一列中。我想念什么?
我尝试使用以下代码将所有结果放入数据框的一列。
import pyodbc
import pandas as pd
conn = pyodbc.connect('Driver={SQL Server};'
'Server=server;'
'Database=server;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute("""SELECT [leadid], [CreateDate], [Status Change Count],
[logdate], [statustitle], [groupedstatus], [leadbucket] FROM vel_actions""")
df = pd.DataFrame(cursor.fetchall())
df.head()返回:
0 [2065004, 2018-03-12 03:06:10.0000000, 1, 2018...
1 [2065004, 2018-03-12 03:06:10.0000000, 2, 2018...
2 [2065004, 2018-03-12 03:06:10.0000000, 3, 2018...
3 [2065004, 2018-03-12 03:06:10.0000000, 4, 2018...
4 [2065004, 2018-03-12 03:06:10.0000000, 5, 2018...
如何将数据放入数据框并保留sql表所在的列?
答案 0 :(得分:1)
fetchall()
返回一个元组列表。在其中,将找不到命名列的数据。
sqlite3
代替了pyodbc
,因为pyodbc
没有启用说明性示例。
import pandas as pd
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Create table
c.execute('''CREATE TABLE stocks
(date text, trans text, symbol text, qty_real, price_real)''')
# Insert a row of data
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
# Save (commit) the changes
conn.commit()
c = conn.cursor()
c.execute("select date, trans, symbol, qty_real, price_real from stocks")
list_o_tuples = c.fetchall()
df = pd.DataFrame(list_o_tuples)
df.columns = ["date", "trans", "symbol", "qty_real", "price_real"]
print(df)
对于sqlite3,您可以让它返回列名,该列名可以传递到您的DataFrame
构造函数中。
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute("select date, trans, symbol, qty_real, price_real from stocks")
list_o_dict_like_rows = c.fetchall()
l = [dict(i) for i in list_o_dict_like_rows]
df = pd.DataFrame(l)
print(df)
conn.close()
对于pyodbc
,答案(此处无法复制)类似于:
cursor = conn.cursor()
cursor.execute("""SELECT [leadid], [CreateDate], [Status Change Count],
[logdate], [statustitle], [groupedstatus], [leadbucket] FROM vel_actions""")
columns = [column[0] for column in cursor.description]
df = pd.DataFrame(cursor.fetchall(), columns=columns)