繁体   English   中英

如何从Datareader pandas中挑选出单独的数值列?

[英]how to pick out individual columns of numerical values from Datareader pandas?

import pandas.io.data as web
import datetime
import matplotlib.pyplot as plt


start = datetime.datetime.strptime('2/10/2016', '%m/%d/%Y')
end = datetime.datetime.strptime('2/24/2016', '%m/%d/%Y')
f = web.DataReader(['GOOG','AAPL'], 'yahoo', start, end)
#print 'Volume'


    wha = f[['Adj Close']]   #pick out Adj Close
    x=wha[0,:]               
    print x.shape      


ax = f['Adj Close'].plot(grid=True, fontsize=10, rot=45.)
ax.set_ylabel('Adjusted Closing Price ($)')
plt.legend(loc='upper center', ncol=2, bbox_to_anchor=(0.5,1.1), shadow=True, fancybox=True, prop={'size':10})
#plt.show()

正如您在上面所看到的,我正在尝试为数据操作选择单个股票价格的数值。

#print wha[1,:]
x=wha[0,:]
print x.shape    

我可以把它归结为一个9x2矩阵,你有两列用于GOOG和AAPL,每个有9个价格。

我试过了

print type(x)

并且看到它

<class 'pandas.core.frame.DataFrame'>

并通过

wha2=x.values.tolist()

我能够挑出股票价格。

我现在有一个简单的方法来计算一只股票(例如AAPL)和Dates的价格吗?

数据操作比Pandas数据帧更容易处理?!?

>>> f['Adj Close'].iloc[:8, :2]
                 AAPL        GOOG
Date                             
2016-02-10  94.269997  684.119995
2016-02-11  93.699997  683.109985
2016-02-12  93.989998  682.400024
2016-02-16  96.639999  691.000000
2016-02-17  98.120003  708.400024
2016-02-18  96.260002  697.349976
2016-02-19  96.040001  700.909973
2016-02-22  96.879997  706.460022

从面板数据中,我首先选择Adj Close列。 然后我使用iloc进行基于索引的位置过滤,选择0-8行和0-1行。

为了获得苹果公司的支持:

>>> f['Adj Close'].loc[:, 'AAPL']
Date
2016-02-10    94.269997
2016-02-11    93.699997
2016-02-12    93.989998
2016-02-16    96.639999
2016-02-17    98.120003
2016-02-18    96.260002
2016-02-19    96.040001
2016-02-22    96.879997
2016-02-23    94.690002
Name: AAPL, dtype: float64

这是文档中索引的链接。 http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-and-selecting-data

>>> f['Adj Close'].corr()
         AAPL     GOOG
AAPL  1.00000  0.87332
GOOG  0.87332  1.00000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM