sqlalchemy連結MySQL時,必須先安裝另一模組 pymysql
pip install pymysql
接下來利用 python 來讀取先前我們用
R寫入的股價資料庫吧
%matplotlib inline
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine("mysql+pymysql://stockuser:password@127.0.0.1:3306/stockdb")
sql ="select * from tw1101;"
df = pd.read_sql_query(sql, engine)
print(df)
df.close.plot()
執行結果:
row_names date open high low close \
0 1 2009-01-02 24.193001 24.193001 24.193001 24.193001
1 2 2009-01-05 25.135599 25.225300 24.597000 24.821400
2 3 2009-01-06 24.955999 25.135599 24.372499 24.641800
3 4 2009-01-07 25.090700 25.449800 24.417400 25.225300
4 5 2009-01-08 24.372499 24.731600 23.789000 24.417400
5 6 2009-01-09 24.552099 25.090700 23.789000 23.789000
...
volume adjusted
0 0.0 14.254436
1 22712510.0 14.624688
2 20298561.0 14.518868
3 26370751.0 14.862664
4 23748492.0 14.386651
...
2536 30751985.0 41.000000
2537 23165336.0 41.599998
2538 16390191.0 41.549999
2539 13459252.0 41.799999
2540 11300709.0 41.750000
[2541 rows x 8 columns]
沒有留言:
張貼留言