当前位置:网站首页>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()

边栏推荐
- Text editor of QT project practice ---------- episode 8
- 热力图展示相关矩阵
- Redis + Lua implementation of distributed interface current limiting
- Thermodynamic diagram display correlation matrix
- Previous basic review
- Go language operators (under Lesson 8)
- Golang example renewal lock: redis+channel+sync Mutex
- 【无标题】
- Rich text tables, lists, pictures
- Scala sample class case calculate
猜你喜欢

Première application de l'informatique quantique à la modélisation des flux de puissance dans les systèmes énergétiques à l'Université technique danoise

程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?

Using tcp/udp tools to debug the yeelight ribbon

4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?

Simulation questions and answers of the latest national fire facility operator (senior fire facility operator) in 2022

Mobile security tool jar

汇编语言(2)基础知识-debug

I 刷题 I — 复制带随机指针的链表

The acceleration of 100 km is only 5.92 seconds, and the willanda high-performance version leads with the strength of high-energy products
![[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem](/img/01/5ec4e5dae1748dce3d5dee33a24b0b.png)
[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem
随机推荐
Première application de l'informatique quantique à la modélisation des flux de puissance dans les systèmes énergétiques à l'Université technique danoise
LLVM TargetPassConfig
[untitled]
EVM Brief
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
Scala object blending trait
我想问一下兴业证券怎么开户?通过链接办理股票开户安全吗
108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)
Can communication experiment between C and C
ImageView shows network pictures
QT (35) - operate excel qxlsx qaxobject
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)
adb shell getevent
最新QQ微信域名防红PHP程序源码+强制跳转打开
A website for programmers with a monthly salary of 30K
VB 学习笔记
adb shell getevent
[redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly
Scala sample class case calculate
腾讯云WeCity解决方案