cody
cody

Reputation: 3

Pandas KeyError: Predictions not in index

I have the following code,

train = data[:training_data_len]
valid = data[training_data_len:]
valid ['Prediction'] = predictions
plt.figure(figsize = (16,8))
plt.title('Model')
plt.xlabel('Date', fontsize=18)
plt.ylabel('Close Price USD ($)', fontsize=18)
plt.plot(train['Close'])
plt.plot(valid[['Close', 'Predictions']])
plt.legend(['Train','Val', 'Predictions'], loc= 'lower right')
plt.show()

I'll get the following error:

KeyError: "['Predictions'] not in index"

It seems to have a very easy fix, but I'm just too new to Python to know how to fix it.

Upvotes: 0

Views: 518

Answers (1)

Fiona De La Fuente
Fiona De La Fuente

Reputation: 36

This question is a little vague so if you could tell us which line is throwing the error you might get better responses. But based on what you wrote here, I noticed that you named the field in the valid dataframe 'Prediction' (singular) and later in the code you start referring to 'Predictions' (plural). Maybe that's your problem.

Upvotes: 1

Related Questions