熊猫keyError:('%s not in index'%objarr [mask])

时间:2018-07-12 19:53:06

标签: python pandas dataframe machine-learning indexing

我是机器学习领域的初学者。我从下面的代码开始:

#import modules
import pandas as pd
import quandl

df = quandl.get('WIKI/GOOGL')

#using required data frames
df = df[['Adj.Open','Adj.High','Adj.Low','Adj.Close','Adj.Volume',]]

#getting percent change in High and Low at closing
df['HL_PCT'] = (df['Adj.High'] - df['Adj.Close']) / df['Adj.Close'] * 100.0

#getting percent change in price
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())

但是我收到索引错误:

raise KeyError('%s not in index' % objarr[mask])

KeyError: "['Adj.Open' 'Adj.High' 'Adj.Low' 'Adj.Close' 'Adj.Volume'] not in index"

该如何解决?

2 个答案:

答案 0 :(得分:3)

在您的API源数据中,.

后面有一个白色的空白
df[['Adj. Open','Adj. High','Adj. Low','Adj. Close','Adj. Volume']]

答案 1 :(得分:1)

每个.后都有空格,请参见下面的代码

import pandas as pd

import quandl

df = quandl.get('WIKI/GOOGL')

#using required data frames
df=df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]

#getting percent change in High and Low at closing
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Close']) / df['Adj. Close'] * 100.0

#getting percent change in price
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())



          Adj. Close    HL_PCT  PCT_change  Adj. Volume
Date                                                     
2004-08-19   50.322842  3.712563    0.324968   44659000.0
2004-08-20   54.322689  0.710922    7.227007   22834300.0
2004-08-23   54.869377  3.729433   -1.227880   18256100.0
2004-08-24   52.597363  6.417469   -5.726357   15247300.0
2004-08-25   53.164113  1.886792    1.183658    9188600.0