coelidonum
coelidonum

Reputation: 543

The easiest way to read an Access table with Pandas?

I have an access database name DB_IMPORT_2020.accdb. It contains only one table named DB_IMPORT_2020_PM. I've been struggling a lot trying to import that table to Pandas. What I've been doing so far is:

# 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)

Then I come to the situation where I have a pandas dataframe with a single column and a list in each row.

0
________________________________________________________
0   [86232, 2019-09-12, INTERNET, ... , N ]
1   [86233, 2019-09-12, INTERNET, ... , M ]
2   [86234, 2019-09-12, MEZZO LIBERO, ...  , Q ]
3   ...

I feel like this is not the right way to do it and it is overly complicated. Does anyone know a simpler way to read data in a table of Access with Pandas?

This is the list i get with 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),...]

Upvotes: 4

Views: 8020

Answers (2)

Gord Thompson
Gord Thompson

Reputation: 123519

The easiest way to work with an Access database and pandas is to use the sqlalchemy-access dialect (which I maintain).

Does anyone know a simpler way to read data in a table of Access with Pandas?

Just use pandas' read_sql_table method:

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)

Upvotes: 4

Let's try
Let's try

Reputation: 1058

Your data is a list of tuples, you need to add the columns when creating the dataframe as described here:

df = pd.DataFrame(data,columns = ["col1","col2",...,"coln"])

Upvotes: 0

Related Questions