Last active
February 14, 2020 00:24
-
-
Save a-poor/8981ca1487d818ee7787f9be60973937 to your computer and use it in GitHub Desktop.
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
# Imports | |
import pandas as pd | |
from sqlalchemy import create_engine | |
# This CSV doesn't have a header so pass | |
# column names as an argument | |
columns = [ | |
"sepal_length", | |
"sepal_width", | |
"petal_length", | |
"petal_width", | |
"class" | |
] | |
# Instantiate sqlachemy.create_engine object | |
engine = create_engine('postgresql://postgres:my_password@localhost:5432/iris') | |
# Create an iterable that will read "chunksize=1000" rows | |
# at a time from the CSV file | |
for df in pd.read_csv("iris.csv",names=columns,chunksize=1000): | |
df.to_sql( | |
'iris_dataset', | |
engine, | |
index=False, | |
if_exists='append' # if the table already exists, append this data | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment