KeyError: 'Close'

Course Name: Python for Trading: Basic, Section No: 6, Unit No: 4, Unit type: Notebook



Instead of nifty csv, i am having infy csv and i am able to print the infy.head().

import pandas as pd

infy = pd.read_csv ('infy.csv')

infy.head ()



but when i am calling BB function,

Calling Bollinger Bands for 'Nifty' index price data

n = 21  # We have kept the window of the moving average as 21 days

Calling the Bollinger Bands function cerated by us

infy_bb = Bollinger_Bands(infy, n)

infy_bb.tail()

i am getting key close error

 

KeyError: 'Close'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-7-eaa8b1711106> in <module>
      4 
      5 # Calling the Bollinger Bands function cerated by us
----> 6 infy_bb = Bollinger_Bands(infy, n)
      7 
      8 infy_bb.tail()


 

When you print the infy.csv file, you will find no 'Close' column in that.



But while calculating Bollinger Band, the 'Close' column name is used.



def Bollinger_Bands(data, n):



    # Calculating the moving average

    MA = data['Close'].rolling(window=n).mean()



    # Calculating the standard deviation

    SD = data['Close'].rolling(window=n).std()



Instead of the 'Close' column if you use the column name that is present in the infy.csv file then this key error can be avoided.



Hope that helps.