Skip to content

Instantly share code, notes, and snippets.

@lucidfrontier45
Last active August 29, 2015 14:06
Show Gist options
  • Save lucidfrontier45/4414d0e270565d4a2298 to your computer and use it in GitHub Desktop.
Save lucidfrontier45/4414d0e270565d4a2298 to your computer and use it in GitHub Desktop.
import pandas as pd
import sqlalchemy
from sklearn import datasets
# create iris dataframe
iris = datasets.load_iris()
iris_data = pd.DataFrame(iris.data)
iris_data.columns = ["sep_length", "sepal_width","petal_length","petal_width" ]
iris_data["species"] = map(lambda i:iris.target_names[i], iris.target)
#insert into sqlite
engine = sqlalchemy.create_engine("sqlite:///test_db")
iris_data.to_sql("iris_dat", engine, index=False, if_exists="replace")
# insert into postgresql
engine = sqlalchemy.create_engine("postgresql://<user>:<passwd>@localhost/test_db")
iris_data.to_sql("iris_dat", engine, index=False, if_exists="replace")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment