当前位置:网站首页>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 border format and text block of rich text
- 对技术的乐观,正让戴尔取得比想象中更多的成就
- Convolution and transpose convolution
- Leetcode 1248. Statistics of "graceful subarray" (harm, suddenly found that it can only enumerate violently)
- 移动安全工具-jar
- Previous basic review (link)
- The interview questions and answers for the high-frequency software test of Dachang help you prepare for the golden nine silver ten
- Cobalt Strike安装教程
- Contentresolver, get the SMS content
- Picture rotation move zoom gradient
猜你喜欢

The interview questions and answers for the high-frequency software test of Dachang help you prepare for the golden nine silver ten

QT(35)-操作EXCEL-QXlsx-QAxObject

丹麦技术大学首创将量子计算应用于能源系统潮流建模

The acceleration of 100 km is only 5.92 seconds, and the willanda high-performance version leads with the strength of high-energy products

热力图展示相关矩阵

2022R1快开门式压力容器操作考题及答案

Xcode preview displays a bug in the content of the list view and its solution

108页(4万字)未来公寓智能化设计平台项目方案建议书2022版

51 single chip microcomputer multi computer communication

Add information on the left and add parts on the right of the status bar
随机推荐
How about compass stock trading software? Is it safe?
Yasea APK Download Image
移动安全工具-jar
Scala adapter pattern
Thingsboard - rest API obtains and refreshes tokens
2022r1 quick opening pressure vessel operation test questions and answers
最新QQ微信域名防红PHP程序源码+强制跳转打开
Input series
2022R1快开门式压力容器操作考题及答案
yasea apk 下载 镜像
腾讯云WeCity解决方案
ImageView展示网络图片
戴尔为何一直拒绝将商用本的超薄推向极致?
卷积与转置卷积
The basic principle and application of iterator and enhanced for
移动安全工具-jarsigner
Default methods for Scala sample classes
ImageView shows network pictures
If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?
Text editor for QT project practice -- Episode 9