简体   繁体   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()

As you can see above, I'm trying to pick out numerical values of individual stock prices for data manipulation. 正如您在上面所看到的,我正在尝试为数据操作选择单个股票价格的数值。

with

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

i could get it down to a 9x2 matrix where you have two columns for GOOG and AAPL and 9 prices each. 我可以把它归结为一个9x2矩阵,你有两列用于GOOG和AAPL,每个有9个价格。

I tried 我试过了

print type(x)

and see that it's 并且看到它

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

and by means of 并通过

wha2=x.values.tolist()

i was able to pick out the stock prices. 我能够挑出股票价格。

Is there an easy way for me to now plot prices of one stock (AAPL alone for example) vs Dates ? 我现在有一个简单的方法来计算一只股票(例如AAPL)和Dates的价格吗?

What more tractable for data manipulation than a Pandas dataframe?!? 数据操作比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

From your panel data, I first select the column Adj Close . 从面板数据中,我首先选择Adj Close列。 I then used iloc for index based location filtering, selecting rows 0-8 and columns 0-1. 然后我使用iloc进行基于索引的位置过滤,选择0-8行和0-1行。

To just get adj close for Apple: 为了获得苹果公司的支持:

>>> 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

Here is a link to indexing in the documentation. 这是文档中索引的链接。 http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-and-selecting-data 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