load csv into mysql_Load CSV data into MySQL using Python, create table and add record

I've never had good luck using the MySQL connector directly either. Now that it's installed, try a combo of sqlalchemy and pandas. Pandas can do the table creation for you, and it will trim your code a lot.

import sqlalchemy

import pandas as pd

# MySQL database connection

engine_stmt = 'mysql+mysqldb://%s:%s@%s:3306/%s' % (username, password,

server,database)

engine = sqlalchemy.create_engine(engine_stmt)

# get your data into pandas

df = pd.read_csv("file/location/name.csv")

# adjust your dataframe as you want it to look in the database

df = df.rename(columns={0: 'yearquarter', 1: 'sms_volumes')

# using your existing function to assign start/end row by row

for index, row in df.iterrows():

dt_start, dt_end = getdatesfromquarteryear(row['yearquarter'])

df.loc[index, 'sms_start_date'] = dt_start

df.loc[index, 'sms_end_date'] = dt_end

# write the entire dataframe to database

df.to_sql(name='sms_volumes', con=engine,

if_exists='append', index=False, chunksize=1000)

print('All data inserted!')

Pandas can make it easy to get the data from your table back into a dataframe, similar to the read_csv():

# create a new dataframe from your existing table

new_df = pd.read_sql("SELECT * FROM sms_volumes", engine)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值