当我尝试仅选择AAPL股票时,我在'Ticker'列上收到KeyError,我不确定为什么...我也尝试使用aapl = all_data.loc[all_data['Ticker'] == 'AAPL']
import pandas_datareader as pdr
import datetime
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def get(tickers, startdate, enddate):
def data(ticker):
return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
datas = map (data, tickers)
return(pd.concat(datas, keys=tickers, names=['Ticker', 'Date']))
tickers = ['AAPL', 'MSFT', 'IBM', 'GOOG']
all_data = get(tickers, datetime.datetime(2006, 10, 1), datetime.datetime(2012, 1, 1))
print(all_data.head())
aapl = all_data[all_data['Ticker'] == 'AAPL']
答案 0 :(得分:1)
这可能是您要寻找的
all_data[all_data.index.get_loc("AAPL")]
输出
High Low Open Close Volume \
Ticker Date
AAPL 2006-10-02 2.709643 2.653571 2.682143 2.673571 712639200.0
2006-10-03 2.676786 2.613929 2.658929 2.645714 790708800.0
2006-10-04 2.695000 2.612857 2.646429 2.692143 829082800.0
2006-10-05 2.720000 2.647500 2.661786 2.672500 683883200.0
2006-10-06 2.680000 2.636071 2.657857 2.650714 466958800.0
... ... ... ... ... ...
2011-12-23 14.413929 14.267500 14.274643 14.404643 269399200.0
2011-12-27 14.610357 14.393572 14.396428 14.518929 265076000.0
2011-12-28 14.580358 14.333571 14.531786 14.380000 228662000.0
2011-12-29 14.487500 14.303928 14.407143 14.468572 215978000.0
2011-12-30 14.510000 14.410357 14.411072 14.464286 179662000.0