一、安裝 tusharepython
pip install tushare
二、啓動ipython數組
C:\Users\Administrator>ipython Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.
三、ts.get_k_data使用幫助網絡
In [1]: import tushare as ts In [2]: ts.get_k_data? Signature: ts.get_k_data(code=None, start='', end='', ktype='D', autype='qfq', index=False, retry_count=3, pause=0.001) Docstring: 獲取k線數據 --------- Parameters: code:string 股票代碼 e.g. 600848 start:string 開始日期 format:YYYY-MM-DD 爲空時取上市首日 end:string 結束日期 format:YYYY-MM-DD 爲空時取最近一個交易日 autype:string 復權類型,qfq-前復權 hfq-後復權 None-不復權,默認爲qfq ktype:string 數據類型,D=日k線 W=周 M=月 5=5分鐘 15=15分鐘 30=30分鐘 60=60分鐘,默認爲D retry_count : int, 默認 3 如遇網絡等問題重複執行的次數 pause : int, 默認 0 重複請求數據過程當中暫停的秒數,防止請求間隔時間過短出現的問題 return ------- DataFrame date 交易日期 (index) open 開盤價 high 最高價 close 收盤價 low 最低價 volume 成交量 amount 成交額 turnoverratio 換手率 code 股票代碼 File: c:\programdata\anaconda3\lib\site-packages\tushare\stock\trading.py Type: function
四、獲取股票信息app
In [3]: df = ts.get_k_data('601318','1999-01-01','2018-1-28') In [4]: df Out[4]: date open close high low volume code 0 2007-03-01 21.254 19.890 21.666 19.469 1977633.51 601318 1 2007-03-02 19.979 19.728 20.166 19.503 425048.32 601318 2 2007-03-05 19.545 18.865 19.626 18.504 419196.74 601318 3 2007-03-06 18.704 19.235 19.554 18.597 297727.88 601318 4 2007-03-07 19.252 19.758 19.936 19.090 287463.78 601318 5 2007-03-08 19.596 19.520 19.694 19.418 130983.83 601318 ... ... ... ... ... ... ... ... 2754 2018-09-27 67.730 67.200 67.750 66.860 623574.00 601318 2755 2018-09-28 67.500 68.500 69.100 67.440 739523.00 601318 [2756 rows x 7 columns]
五、把獲取的數據下載到本地函數
In [5]: df.to_csv('601318.csv')
DataFrame有行索引和列索引spa
DataFrame一樣能夠經過標籤和位置兩種方法進行索引和切片3d
向DataFrame隊形中寫入值時只是用方法2code
行/列索引部分能夠是常規索引、切片、布爾值索引、花式索引任意搭配(注意:兩部分都是花式索引時結果可能與預料的不一樣)orm
df.index
df3 = df2.T
df.columns
df.values
df.describe()
一、用法blog
二、修改全部的列
df = pd.read_csv('601318.csv',header=None,names=list('asdfghjk'))
三、從頭開始修改兩列
df = pd.read_csv('601318.csv',header=None) df = df.rename(columns={0:'a',1:'b'})
df2.mean()
df2.mean(skipna=True)
df2.sort_index(ascending=False)
df2.sort_values('close')
df.apply(lambda x:x.mean(),axis=1)
df.apply(lambda x:x['high'] + x['low']/2,axis=1)
df.apply(lambda x:pd.Series([(x['high'] + x['low'])/2,(x['open'] +x['close'])/2],index=['h1_mean','oc_mean']),axis=1)