Pandas Insert data into MySQL Pandas Insert data into MySQL pandas pandas

Pandas Insert data into MySQL


I think your code should read like this

import pandas as pdfrom pandas.io import sqlfrom sqlalchemy import create_enginedf = pd.read_csv('File.csv', usercols=['ID', 'START_DATE'], skiprows=skip)print(df)engine = create_engine('mysql://username:password@localhost/dbname')with engine.connect() as conn, conn.begin():    df.to_sql('Table1', conn, if_exists='replace')

But, regarding your question, unless I am mistaken in my understanding of Pandas, whatever columns df presently has, those are going to be written to the columns of the same name of the mysql table.

If you need different column names, you'll want to rename those in the DataFrame

Or use the parameters, as mentioned,

index : boolean, default True
Write DataFrame index as a column.

index_label : string or sequence, default None
Column label for index column(s). If None is given (default) and index is True, then the index names are used


This is what i did in my project

 import pandas as pd import sqlalchemy engine = sqlalchemy.create_engine('mysql+pymysql://root:@localhost/pd_test') ratings = pd.read_csv('ratings2.csv', sep='\t', encoding='latin-1',                  usecols=['user_id', 'movie_id', 'user_emb_id',  'movie_emb_id','rating']) ratings.to_sql('test', con=engine, if_exists='append',index=False,chunksize=1)

Hope this help!!