Created
December 25, 2020 10:11
-
-
Save aveek22/32b5487838605700fe37afdb48f9b575 to your computer and use it in GitHub Desktop.
This file exports data from pandas to a table in PostgreSQL database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import sqlalchemy | |
# Create the engine to connect to a PostgreSQL database | |
engine = sqlalchemy.create_engine('postgresql://aveek:test1234@localhost:5432/sql-shack-demo') | |
# Read data from the SQL Table | |
data = pd.read_csv('superstore.csv') | |
# Print first few rows of the dataframe | |
print(data.head()) | |
####################################################################################### | |
# Write the data into a table in PostgreSQL Database | |
data.to_sql(name = 'superstore', | |
con = engine, | |
if_exists='append', | |
index = False, | |
chunksize = 1000, | |
dtype = { | |
"Row_ID" : sqlalchemy.types.Integer, | |
"Order_ID" : sqlalchemy.types.Text, | |
"Order_Date" : sqlalchemy.types.DateTime, | |
"Ship_Date" : sqlalchemy.types.DateTime, | |
"Sales" : sqlalchemy.types.Numeric, | |
"Quantity" : sqlalchemy.types.Integer, | |
"Discount" : sqlalchemy.types.Numeric, | |
"Profit" : sqlalchemy.types.Numeric, | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment