2

I have a Pandas DataFrame with around 200,000 indexes/rows and 30 columns. I need to have this directly exported into an .mdb file, converting it into a csv and manually importing it will not work.

I understand there's tools like pyodbc that help a lot with importing/reading access, but there is little documentation on how to export.

I'd love any help anyone can give, and would strongly appreciate any examples.

2

3 Answers 3

2

First convert the dataframe into .csv file using the below command

name_of_your_dataframe.to_csv("filename.csv", sep='\t', encoding='utf-8')

Then load .csv to .mdb using pyodbc

MS Access can directly query CSV files and run a Make-Table Query(https://support.office.com/en-us/article/Create-a-make-table-query-96424f9e-82fd-411e-aca4-e21ad0a94f1b) to produce a resulting table. However, some cleaning is needed to remove the rubbish rows. Below opens two files one for reading and other for writing. Assuming rubbish is in first column of csv, the if logic writes any line that has some data in second column (adjust as needed):

import os
import csv
import pyodbc

# TEXT FILE CLEAN
with open('C:\Path\To\Raw.csv', 'r') as reader, open('C:\Path\To\Clean.csv', 'w') as writer:
    read_csv = csv.reader(reader); write_csv = csv.writer(writer,lineterminator='\n')

    for line in read_csv:
        if len(line[1]) > 0:            
            write_csv.writerow(line)

# DATABASE CONNECTION
access_path = "C:\Path\To\Access\\DB.mdb"
con = pyodbc.connect("DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};DBQ={};" \
                 .format(access_path))

# RUN QUERY
strSQL = "SELECT * INTO [TableName] FROM [text;HDR=Yes;FMT=Delimited(,);" + \
     "Database=C:\Path\To\Folder].Clean.csv;"    
cur = con.cursor()
cur.execute(strSQL)
con.commit()

con.close()                            # CLOSE CONNECTION
os.remove('C\Path\To\Clean.csv')       # DELETE CLEAN TEMP 
Sign up to request clarification or add additional context in comments.

Comments

2

2020 Update

There is now a supported external SQLAlchemy dialect for Microsoft Access ...

https://github.com/gordthompson/sqlalchemy-access

... which enables you to use pandas' to_sql method directly via pyodbc and the Microsoft Access ODBC driver (on Windows).

Comments

0

I would recommend to export the pandas dataframe to csv as usual like this:

dataframe_name.to_csv("df_filename.csv", sep=',', encoding='utf-8')

Then you can convert it to .mdb file as this stackoverflow answer shows

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.