当前位置:网站首页>How to store dataframe data in pandas into MySQL
How to store dataframe data in pandas into MySQL
2022-06-25 01:12:00 【A Xiaobai who likes Technology】
problem :
How to make Pandas Medium DataFrame Data import to MySQL In the database ?
So how to achieve it ?
Implementation code :
To prevent code redundancy, I wrote a class directly
When using, you only need to modify the passed parameters
import pymysql
from sqlalchemy import create_engine
import pandas as pd
""" Paste directly into brackets host='127.0.0.1', port='3306', user="root", pasword="123456", db=" Database name ", tb=" Table name ", df="dataframe data " """
#pandas dataframe Data is loaded directly into mysql in
class PDTOMYSQL:
def __init__(self,host,user,pasword,db,tb,df,port='3306'):
self.host = host
self.user = user
self.port = port
self.db = db
self.password = pasword
self.tb = tb
self.df = df
sql = 'select * from '+self.tb
conn = create_engine('mysql+pymysql://'+self.user+':'+self.password+'@'+self.host+':'+self.port+'/'+self.db)
df.to_sql(self.tb, con=conn, if_exists='replace')
self.pdata = pd.read_sql(sql,conn)
def show(self):# Show datasets
return print(self.pdata)
Test it :
#data It's test data
data = {
'state':['Ohio','Ohio','Ohio','Nevada','Nevada'],
'year':[2000,2001,2002,2001,2002],
'pop':[1.5,1.7,3.6,2.4,2.9]
}
# hold data convert to dataframe data
frame = pd.DataFrame(data)
t = PDTOMYSQL(
host='127.0.0.1',
port='3306',
user="root",
pasword="123456",
db="data_base",
tb="t_demo",
df=frame)
t.show()
边栏推荐
- QT electronic clock
- Can communication experiment between C and C
- Golang示例续期锁:Redis+Channel+sync.Mutex
- Scala template method pattern
- 图书馆管理系统代码源码(php+css+js+mysql) 完整的代码源码
- Sanic服务启动失败
- 4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
- 移动安全工具-jarsigner
- A plug-in framework for implementing registration free and login verification with hook technology
- A website for programmers with a monthly salary of 30K
猜你喜欢
Simulation questions and answers of the latest national fire facility operator (senior fire facility operator) in 2022
Syntax highlighting of rich text
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
Add information on the left and add parts on the right of the status bar
Cobalt Strike安装教程
The acceleration of 100 km is only 5.92 seconds, and the willanda high-performance version leads with the strength of high-energy products
51 single chip microcomputer multi computer communication
Preliminary understanding of qtoolbutton
Scala IO read by character
LLVM TargetPassConfig
随机推荐
这个国庆!腾讯云WeCity陪您一同出行,点亮城市地标...
移动安全工具-apktool
利用 Redis 的 sorted set 做每周热评的功能
2022 simulated 100 questions of safety officer-c certificate examination and online simulated examination
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
2022安全员-C证考试模拟100题及在线模拟考试
4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
A small crawler program written by beginners
Text border format and text block of rich text
Add information on the left and add parts on the right of the status bar
丹麦技术大学首创将量子计算应用于能源系统潮流建模
QT (35) - operate excel qxlsx qaxobject
热力图展示相关矩阵
Scala IO reads by lexical units and numbers
QT(35)-操作EXCEL-QXlsx-QAxObject
JSON file of China's provincial boundaries
Transform BeanUtils to achieve list data copy gracefully
I'd like to ask how to open an account at industrial securities? Is it safe to open a stock account through the link
腾讯云WeCity解决方案
Scala trait inheritance class