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

边栏推荐
- 新手看过来,带你一次性了解“软考”
- Source code analysis the problem that fragments cannot be displayed in the custom ViewGroup
- Scala sample object
- Library management system code source code (php+css+js+mysql) complete code source code
- 卷积与反卷积关系超详细说明及推导(反卷积又称转置卷积、分数步长卷积)
- EVM Brief
- 网上开户选哪个证券公司?网上开户安全么?
- 【无标题】
- Using tcp/udp tools to debug the yeelight ribbon
- 移动安全工具-dex2jar
猜你喜欢

Text border format and text block of rich text

重磅:国产IDE发布,由阿里研发,完全开源!(高性能+高定制性)

2022熔化焊接与热切割复训题库模拟考试平台操作

Thermodynamic diagram display correlation matrix

Databinding quick start (still using findviewbyid?)

利用 Redis 的 sorted set 做每周热评的功能

Library management system code source code (php+css+js+mysql) complete code source code

汇编语言(4)函数传参

Danish Technical University pioneered the application of quantum computing to power flow modeling of energy system

Cobalt Strike安装教程
随机推荐
ContentResolver,拿到手机短信内容
2022 simulated 100 questions of safety officer-c certificate examination and online simulated examination
2022 crane driver (limited to bridge crane) examination question bank simulated examination platform operation
2022R1快开门式压力容器操作考题及答案
Scala sample class case calculate
2022安全员-C证考试模拟100题及在线模拟考试
Punch smart spirit 1. The brand is attractive. What is the strength of the product?
Première application de l'informatique quantique à la modélisation des flux de puissance dans les systèmes énergétiques à l'Université technique danoise
Scala trait inheritance class
adb shell sendevent
bindservice方法实现音乐播放暂停
Golang example renewal lock: redis+channel+sync Mutex
activity生命周期
中金财富证券开户佣金多少呢?股票开户交易安全靠谱吗?
Easy to wear - drop down radio
图书馆管理系统代码源码(php+css+js+mysql) 完整的代码源码
Mobile security tool jarsigner
我想问一下兴业证券怎么开户?通过链接办理股票开户安全吗
Using bindservice method to pause music playing
EVM Brief