Memo to create a table from pandas DataFrame DataFrame could not be inserted into postgres when to_sql as it is
before.py
import pandas as pd
import psycopg2
db = "dbname=postgres host=localhost user=username"
conn = psycopg2.connect(db)
df = pd.read_csv("sample.csv")
df.to_sql(df,"table_name", conn, if_exist="replace", index=False)
As a result of various investigations, it seems that writing to sqlite is the default, so I changed it
after.py
import pandas as pd
from sqlalchemy import create_engine
df = pd.read_csv("sample.csv")
engine=create_engine("postgresql://username@hostname:port/dbname")
df.to_sql("table_name", engine, if_exist="replace", index=False)
read_sql was fine with the before method
Reference> http://stackoverflow.com/questions/23103962/how-to-write-dataframe-to-postgres-table