2017-04-02 92 views
0

我使用python 3.5.1,显示如下数据大熊猫错误在Python 3.5.1

import pandas as pd 
import quandl 
df = quandl.get('WIKI/GOOGL') 
print(df.head()) 

我做以下

import pandas as pd 
import quandl 
df = quandl.get('WIKI/GOOGL') 

df = df[['Adj. Open','Adj. High','Adj. Low','Adj. Close','Adj. Volume',]] 
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Close'])/df['Adj. Close']*100.0 
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open'])/df['Adj. OPen']*100.0 
df = df[['Adj. Close','HL_PCT','PCT_change','Adj. Volume']] 

print(df.head()) 

但其表现以下错误

回溯(最近调用最后一次): get_loc 返回self文件“C:\ Users \ jayram \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ pandas \ indexes \ base.py”,第2134行。 _发动机。 get_loc(key) pandas.index.IndexEngine.get_loc(pandas \ index.c:4433)中的文件“pandas \ index.pyx”,第132行, pandas中的文件“pandas \ index.pyx”,行154。 index.indexEngine.get_loc(pandas \ index.c:4279) pandas.hashtable.PyObjectHashTable.get_item(pandas \ hashtable.c:13742)中的文件“pandas \ src \ hashtable_class_helper.pxi”,第732行 文件“pandas \ src \ hashtable_class_helper.pxi“,第740行,在pandas.hashtable.PyObjectHashTable.get_item(pandas \ hashtable.c:13696) KeyError:'Adj。打开”

在处理上述异常,另一个异常:

回溯(最近通话最后一个): 文件 “C:/用户/ jayram /桌面/蟒蛇/ OK2”,7号线,在 df ['PCT_change'] =(df ['Adj。Close'] - df ['Adj。Open'])/ df ['Adj。 OPen'] * 100.0 文件“C:\ Users \ jayram \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ pandas \ core \ frame.py”,行2059,在getitem return self ._getitem_column(key) _getitem_column 中的文件“C:\ Users \ jayram \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ pandas \ core \ frame.py”,第2066行返回self._get_item_cache (密钥) 值为self._data的文件“C:\ Users \ jayram \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ pandas \ core \ generic.py”,第1386行,位于_get_item_cache 中。 get(item) 获取 文件“C:\ Users \ jayram \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ pandas \ core \ internals.py”,第3543行loc = self.items .get_loc(item) get_loc中的第2136行文件“C:\ Users \ jayram \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ pandas \ indexes \ base.py”返回self._engine.get_loc(self。 _maybe_cast_indexer(key)) pandas.index.IndexEngine.get_loc(pandas \ index.c:4433)中的文件“pandas \ index.pyx”,第132行, pandas文件“pandas \ index.pyx”,第154行.index.IndexEngine.get_loc(pandas \ index.c:4279) pandas.hashtable.PyObjectHashTable.get_item(pandas \ hashtable.c:13742)中的文件“pandas \ src \ hashtable_class_helper.pxi”,第732行 文件“ pandas \ src \ hashtable_class_helper.pxi“,第740行,在pandas.hashtable.PyObjectHashTable.get_item(pandas \ hashtable.c:13696) KeyError:'Adj。开 '退出代码完成

过程1

如何解决上述错误

+3

尝试改变:'DF [' ADJ。打开'])/ df ['Adj。 OPen']' - >'df ['Adj。打开'])/ df ['Adj。打开']' – MaxU

+0

是的作品,是一个小错误 –

回答

1

变化

df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open'])/df['Adj. OPen']*100.0 

df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open'])/df['Adj. Open']*100.0 
+3

这将更好地作为对OP的评论,因为对于保持SO的OP没有什么长期价值,因为它只是一个错字。 –