반응형
필요 패키지
from sqlalchemy import create_engine
import pymysql
import pandas as pd
콘다환경이라면 conda install
로 설치하는 것 권장
DB 연결하기
endpoint = 'endpoint'
schema_name = 'test'
db_connection_str = 'mysql+pymysql://admin:password@{}/{}'.format(endpoint, schema_name )
try :
db_connection = create_engine(db_connection_str)
conn = db_connection.connect()
except :
print('fail to connect db')
엑셀 → 데이터프레임 불러오기
df = pd.read_excel('./전국자연환경조사_4차_포유류_2017_전국.xlsx')
csv, json.. 등 다른 포맷으로 데이터 프레임 불러오기
[pandas] 여러가지 resource를 이용하여 DataFrame 생성하기 (csv, sql, api, json)
데이터프레임 → DB
Tabel_name = '전국자연환경조사_2017_포유류'
df.to_sql(name=Tabel_name, con=db_connection, if_exists='replace', index=False)
# if_exists options : append, fail, replace
DB → 데이터프레임
sql = "SELECT * FROM {}".format(Tabel_name)
df = pd.read_sql(sql, db_connection)
반응형
'Programming > DB' 카테고리의 다른 글
[DB]MySQL workbench 데이터 백업/복원하기 (0) | 2022.01.26 |
---|