方法一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#-*- coding:utf-8 -*- from sqlalchemy import create_engine class mysql_engine(): user = '******' passwd = '******' host = '******' port = '******' db_name = '******' engine = create_engine( 'mysql://{0}:{1}@{2}:{3}/{4}?charset=utf8' . format (user,passwd,host,port,db_name)) def get_data(sql): pg_enine = mysql_engine() try : with pg_enine.engine.connect() as con, con.begin(): df = pd.read_sql(sql,con) # 获取数据 con.close() except : df = None return df |
方法二:
1
2
3
4
|
conn = MySQLdb. connect (host= "******" , user = "******" ,passwd= "******" ,db= '******' ,port = ******,charset= "utf8" ) sql = "select * from ****** limit 3" df = pd.read_sql(sql,conn,index_col= "id" ) print df |
pd 1.9以后的版本,除了sqllite,均需要通过sqlalchemy来设置
以上这篇Pandas读取MySQL数据到DataFrame的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/data_scientist/article/details/54728600