pythonpandas数据库_python-pandas和类似mysql的数据库

The documentation for Pandas has numerous examples of best practices for working with data stored in various formats.

However, I am unable to find any good examples for working with databases like MySQL for example.

Can anyone point me to links or give some code snippets of how to convert query results using mysql-python to data frames in Pandas efficiently ?

解决方案

As Wes says, io/sql's read_sql will do it, once you've gotten a database connection using a DBI compatible library. We can look at two short examples using the MySQLdb and cx_Oracle libraries to connect to Oracle and MySQL and query their data dictionaries. Here is the example for cx_Oracle:

import pandas as pd

import cx_Oracle

ora_conn = cx_Oracle.connect('your_connection_string')

df_ora = pd.read_sql('select * from user_objects', con=ora_conn)

print 'loaded dataframe from Oracle. # Records: ', len(df_ora)

ora_conn.close()

And here is the equivalent example for MySQLdb:

import MySQLdb

mysql_cn= MySQLdb.connect(host='myhost',

port=3306,user='myusername', passwd='mypassword',

db='information_schema')

df_mysql = pd.read_sql('select * from VIEWS;', con=mysql_cn)

print 'loaded dataframe from MySQL. records:', len(df_mysql)

mysql_cn.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值