Skip to content Skip to sidebar Skip to footer

Python: Dataframes To Db

I have the following dictionary of dataframes created from a folder of excel files: import os import glob import pandas as pd files = glob.glob(os.path.join('staging' + '/*.csv'))

Solution 1:

Your dict_ already contains the DataFrames, you need to iterate over your dict_ values:

fordfin dict_.values():
    df.to_sql(...)

If you want to use the key as the table's name, you can try something like:

for key, df in dict_.items(): df.to_sql(key, conn, flavor = None, schema = None, if_exists = 'replace', index = True, index_label = None, chunksize = None, dtype = None)

Also, the first argument of the to_sql method should be the name of the table where you want to insert your data. It should be a str. I don't think that is what you have right now. The con argument should be an SQLAlchemy or pyscopg2 (or others) connexion to your database.

Take a look at the documentation.

Post a Comment for "Python: Dataframes To Db"