Pandas的文档中有许多用于处理以各种格式存储的数据的最佳实践示例.
但是,我无法找到使用MySQL等数据库的任何好例子.
任何人都可以指向我链接或给出一些代码片段,如何使用mysql-python将查询结果转换为Pandas中的数据帧有效吗?
我正在努力实现以下目标.我想创建一个python类,将数据库中的所有表转换为pandas数据帧.
我就是这样做的,这不是很通用的......
class sql2df():
def __init__(self, db, password='123',host='127.0.0.1',user='root'):
self.db = db
mysql_cn= MySQLdb.connect(host=host,
port=3306,user=user, passwd=password,
db=self.db)
self.table1 = psql.frame_query('select * from table1', mysql_cn)
self.table2 = psql.frame_query('select * from table2', mysql_cn)
self.table3 = psql.frame_query('select * from table3', mysql_cn)
Run Code Online (Sandbox Code Playgroud)
现在我可以像这样访问所有表:
my_db = sql2df('mydb')
my_db.table1
Run Code Online (Sandbox Code Playgroud)
我想要的东西:
class sql2df():
def __init__(self, db, password='123',host='127.0.0.1',user='root'):
self.db = db
mysql_cn= MySQLdb.connect(host=host,
port=3306,user=user, passwd=password,
db=self.db)
tables = (""" SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = '%s' """ % self.db)
<some kind of iteration that gives back all the …Run Code Online (Sandbox Code Playgroud)