如何从 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)与日期的价格?
还有什么比 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
根据您的面板数据,我首先 select 列 Adj Close
。然后我使用 iloc
进行基于索引的位置过滤,selecting 行 0-8 和列 0-1。
要为 Apple 关闭 adj:
>>> 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
这里是 link 文档中的索引。
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
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)与日期的价格?
还有什么比 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
根据您的面板数据,我首先 select 列 Adj Close
。然后我使用 iloc
进行基于索引的位置过滤,selecting 行 0-8 和列 0-1。
要为 Apple 关闭 adj:
>>> 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
这里是 link 文档中的索引。 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