一、 写入MySQL数据库import numpy as np
import pandas as pd
import pymysql
from sqlalchemy import create_engine
#连接数据库的参数db_info = {
'user': 'root',
'password': 'emtf',
'host': 'localhost',
'database': 'test'
}
#创建连接数据库引擎engine = create_engine('mysql://%(user)s:%(password)s@%(host)s/%(database)s?charset=utf8'
% db_info, encoding='utf-8')
#创建数据import seaborn as sns
titanic = sns.load_dataset('titanic')
#将数据写入test数据库的titanics表中titanic.to_sql('titanics', con=engine, if_exists='append', index=False)#if the tabel don't exist, create it
二、 读MySQL数据库sql = "SELECT * FROM {0} where sex = '{1}' and class ='{2}';".format('titanics', 'female', 'First')
df = pd.read_sql(sql, con=engine)
df.head(12)

本文介绍如何使用Python语言中的pandas、numpy等库配合pymysql进行MySQL数据库的操作,包括数据写入和读取的过程。示例代码展示了从加载数据集到将其写入MySQL数据库,再到根据特定条件查询数据的具体步骤。
611

被折叠的 条评论
为什么被折叠?



