用 Pandas 读取 Access 表的最简单方法是啥?
Posted
技术标签:
【中文标题】用 Pandas 读取 Access 表的最简单方法是啥?【英文标题】:The easiest way to read an Access table with Pandas?用 Pandas 读取 Access 表的最简单方法是什么? 【发布时间】:2020-12-13 23:36:55 【问题描述】:我有一个访问数据库名称 DB_IMPORT_2020.accdb。它仅包含一个名为 DB_IMPORT_2020_PM 的表。我一直在努力尝试将该表导入 Pandas。 到目前为止,我一直在做的是:
# define components of our connection string
driver = 'Microsoft Access Driver (*.mdb, *.accdb)'
filepath = r"C:\Users\corra\Desktop\DB_IMPORT_2020.accdb"
# create a connection to the database
cnxn = pyodbc.connect(driver = driver, dbq = filepath, autocommit = True)
crsr = cnxn.cursor()
# define the components of a query
table_name = 'DB_IMPORT_2020_PM'
# define query
query = "SELECT * FROM ".format(table_name)
# execute the query
crsr.execute(query)
data = crsr.fetchall()
df = pd.DataFrame(data)
然后我遇到了这样一种情况,即我有一个 pandas 数据框,它只有一列,每一行都有一个列表。
0
________________________________________________________
0 [86232, 2019-09-12, INTERNET, ... , N ]
1 [86233, 2019-09-12, INTERNET, ... , M ]
2 [86234, 2019-09-12, MEZZO LIBERO, ... , Q ]
3 ...
我觉得这不是正确的方法,而且过于复杂。 有谁知道用 Pandas 读取 Access 表中数据的更简单方法?
这是我使用 data = crsr.fetchall() 得到的列表
[(86232, datetime.datetime(2019, 9, 12, 0, 0), 'INTERNET', 'A.M Web', 'Brand_SMX', 0.0, 'gen', '20_FCST', 'OnLine', 'dipendente s', 'Low Rev.', 'STX', 'A.M', 'INTERNET', 'Brand_SMX', 'dipendente s', 'STORICI', 'TIER 1', 1.0, 'TIER 1', 'ALIMENTARI', '04_SRF', 'SMX', 'ALTRI', 'STC', 'Reservation', 'Off + On', 'Online_Res', 'TIER 1', None, None, None, None),
(86233, datetime.datetime(2019, 9, 12, 0, 0), 'INTERNET', 'A.M Web', 'Brand_SMX', 0.0, 'feb', '20_FCST', 'OnLine', 'dipendente s', 'Low Rev.', 'STC', 'A. M', 'INTERNET', 'Brand_SMX', 'dipendente s', 'STORICI', 'TIER 1', 1.0, 'TIER 1', 'ALIMENTARI', '04_SRF', 'SMX', 'ALTRI', 'STX', 'Reservation', 'Off + On', 'Online_Res', 'TIER 1', None, None, None, None),
(86234, datetime.datetime(2019, 9, 12, 0, 0), 'MEZZO LIBERO', 'S ITALIA SRL', 'S ELECTRONICS', 0.0, 'gen', '20_FCST', 'OffLine', 'BO / CI', 'Low Rev.', 'STX', 'S Italia Srl', 'MEZZO LIBERO', 'S', 'BEN BOT', 'STORICI', 'INTERCx', 1.0, 'INTERCx', 'INFORMATICA/FOTOGRAFIA', '04_SRF', 'SMX', 'ALTRI', 'STC', 'Reservation', 'Off + On', 'Offline_Res', 'INTX', None, None, None, None),...]
【问题讨论】:
data = crsr.fetchall()
是什么类型?
类型为列表
我认为df = pd.DataFrame(data[0])
可以解决问题。否则可以添加data
的打印吗?
不行,我已经添加了列表的前两行。
【参考方案1】:
您的数据是tuples
的list
,您需要在创建数据框时添加列,如here 所述:
df = pd.DataFrame(data,columns = ["col1","col2",...,"coln"])
【讨论】:
我收到此错误 ValueError: Shape of passed values is (1914390, 1), indices imply (1914390, 33) 。我有 33 列并添加了 df = pd.DataFrame(data,columns = ["col1","col2","col3","col4","col5","col6","col7","col8"," col9","col10","col11","col12","col13","col14","col15","col16","col17","col18","col19","col20","col21" "col22","col23","col24","col25","col26","col27","col28","col29","col30","col31","col32","col33"]) 如果我更改为 df = pd.DataFrame([data],columns = ["col1","col2","col3","col4","col5","col6"," col7","col8","col9","col10","col11","col12","col13","col14","col15","col16","col17","col18","col19" "col20","col21","col22","col23","col24","col25","col26","col27","col28","col29","col30","col31"," col32","col33"]) 我得到这个错误:ValueError: 33 columns passed, pass data has 1914390 columns 实际上,我已经尝试过pd.DataFrame(data)
,它适用于您提供的示例数据。 df = pd.DataFrame(data[0])
会出现什么错误?您是否有任何带有'
或"
的字段以及里面的逗号或类似的东西?否则真的不知道发生了什么。【参考方案2】:
使用 Access 数据库和 pandas 的最简单方法是使用 sqlalchemy-access 方言(我维护)。
有人知道用 Pandas 读取 Access 表中数据的更简单方法吗?
只需使用 pandas 的read_sql_table 方法:
import pandas as pd
import sqlalchemy as sa
table_name = 'DB_IMPORT_2020_PM'
engine = sa.create_engine("access+pyodbc://@my_accdb_dsn")
df = pd.read_sql_table(table_name, engine)
【讨论】:
您可能希望在此答案中披露从属关系并添加更多内容(如何使用它的快速示例),如 How to not be a spammer 所述。不过仍然是一个很好的答案。以上是关于用 Pandas 读取 Access 表的最简单方法是啥?的主要内容,如果未能解决你的问题,请参考以下文章
Python Pandas - 读取包含多个表的 csv 文件