Skip to content

Instantly share code, notes, and snippets.

@a-poor
Last active February 14, 2020 00:25
Show Gist options
  • Save a-poor/1903cf2c89400c63bf5516c8caf32c8d to your computer and use it in GitHub Desktop.
Save a-poor/1903cf2c89400c63bf5516c8caf32c8d to your computer and use it in GitHub Desktop.
# 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"
]
# Load in the data
df = pd.read_csv(
"iris.csv",
names=columns,
nrows=1000 # load in the first 1000 rows
)
# Instantiate sqlachemy.create_engine object
engine = create_engine('postgresql://postgres:my_password@localhost:5432/iris')
# Save the data from dataframe to
# postgres table "iris_dataset"
df.to_sql(
'iris_dataset',
engine,
index=False
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment