[python] Create table from pandas DataFrame to postgres

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

Recommended Posts

[python] Create table from pandas DataFrame to postgres
Create folders from '01' to '12' with python
Create a pandas Dataframe from a string.
[Python] Add total rows to Pandas DataFrame
Create a dataframe from excel using pandas
[Python] How to add rows and columns to a table (pandas DataFrame)
Edit Excel from Python to create a PivotTable
Changes from Python 3.0 to Python 3.5
[Python / Tkinter] Search for Pandas DataFrame → Create a simple search form to display
[Python] Sort the table by sort_values (pandas DataFrame)
Python application: Pandas # 3: Dataframe
[Python] Summary of table creation method using DataFrame (pandas)
Python script to create a JSON file from a CSV file
How to create a kubernetes pod from python code
Post from Python to Slack
[Python] Convert list to Pandas [Pandas]
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Export pandas dataframe to excel
I wrote Python code to create a table (view) dependency diagram (PlantUML) from SQL
[Python] How to output a pandas table to an excel file
[Python] Random data extraction / combination from DataFrame using random and pandas
[Python] How to create a table from list (basic operation of table creation / change of matrix name)
Call Matlab from Python to optimize
Python hand play (Pandas / DataFrame beginning)
[Python] Operation memo of pandas DataFrame
Hit treasure data from Python Pandas
[Python] How to use Pandas Series
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Convert list to DataFrame with python
Connect to utf8mb4 database from python
[Introduction to Python] Let's use pandas
Python (from first time to execution)
Post images from Python to Tumblr
How to access wikipedia from python
Python to switch from another language
[Introduction to Python] Let's use pandas
5 Ways to Create a Python Chatbot
[Introduction to Python] Let's use pandas
[Python] Create API to send Gmail
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
python / pandas / dataframe / How to get the simplest row / column / index / column
[Python] How to get & change rows / columns / values from a table.
Various ways to create an array of numbers from 1 to 10 in Python.
DataFrame of pandas From creating a DataFrame from two lists to writing a file
Python hand play (CSV is applied with Pandas DataFrame, but only full-column Insert from CSV to DB?)
[Python] Fluid simulation: From linear to non-linear
How to create a clone from Github
[Python] What is pandas Series and DataFrame?
[Python] Summary of how to use pandas
[Python] Use pandas to extract △△ that maximizes ○○
From Python to using MeCab (and CaboCha)
Create wordcloud from your tweet with python3
How to update Google Sheets from Python
A story I was addicted to when inserting from Python to a PostgreSQL table
Private Python handbook (updated from time to time)
I want to use jar from python