我有一个名为DB_IMPORT_2020.accdb的Access数据库,它只包含一个名为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表格中读取数据的更简单方法?
这是使用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()
是什么类型? - Let's trydf = pd.DataFrame(data[0])
可以解决问题。否则,请添加data
的打印输出? - Let's try