Pandas+MySQLdb——python3環境配置(OS系統)

Pandas+MySQLdb——python3環境配置(OS系統)

來自專欄新交通人的技術閑談1 人贊了文章

一分鐘讀完全文

對dataframe結構數據的導入導出mysql進行了介紹,包括對mysqlclient的安裝(MySQLdb在python3上的替代),以及如何利用pandas自帶的to_sql快速導入數據。

安裝包:Python 3 ImportError: No module named ConfigParser

一開始天真的以為pip就能解決的:

sudo pip install MySQL-python

結果,又又又又又又又又又出現的Python 3 ImportError: No module named ConfigParser是什麼鬼?

SO上如是說:In Python 3,ConfigParserhas been renamed toconfigparserfor PEP 8 compliance. It looks like the package you are installing does not support Python 3.

也就是說,PY3不支持,不支持啊!!

不要慌,可以拿mysqlclient代替:

brew install mysqlpip install mysqlclient

So, what is the mysqlclinet ??

---It is a fork ofMySQL-pythonwith added support for Python 3.

不會brew的請參考我之前文章:Python之美——一隻數據狗的筆記[長期更新]

DataFrame怎麼直接導入mysql

from sqlalchemy import create_engineimport pandas as pdimport numpy as npnp.random.seed(0)number_of_samples = 10frame = pd.DataFrame({ feature1: np.random.random(number_of_samples), feature2: np.random.random(number_of_samples), class: np.random.binomial(2, 0.1, size=number_of_samples), },columns=[feature1,feature2,class])engine = create_engine(mysql://username:password@host/dbname)frame.to_sql(con=engine, name=table_name_for_df, if_exists=replace)

註:mysql.connector雖然也可以連接、插入數據,但好像還不被DF裡面的to_sql所支持。

參考:How to insert pandas dataframe via mysqldb into database?

讀取出來的數據怎麼格式化為DataFrame

engine = create_engine(mysql://username:password@host/dbname)connection = engine.connect()resoverall = connection.execute("SELECT * FROM sys.table_name_for_df")df = pd.DataFrame(resoverall.fetchall())df.columns = resoverall.keys()

參考:How to convert SQL Query result to PANDAS Data Structure?

推薦閱讀:

簡單說明下MySQL的內存使用
MySQL5.7綠色版安裝
一個MySQL語句的優化
在MySQL中,排序後添加序號列
想非常精通資料庫的話需要哪方面數學?

TAG:MySQL | pandas包 | 資料庫 |