Simbi Simbi - 4 months ago 17
Python Question

KeyError when indexing Pandas dataframe

I am trying to read data from a csv file into a pandas dataframe, and access the first column 'Date'

df_ticks=pd.read_csv('values.csv', delimiter=',')
print df_ticks.columns
df_ticks['Date']


produces the following result

Index([u'´╗┐Date', u'Open', u'High', u'Low', u'Close', u'Volume'], dtype='object')
KeyError: u'no item named Date'


If I try to acces any other column like 'Open' or 'Volume' it is working as expected

Answer

You most likely have an extra character at the beginning of your file, that is prepended to your first column name, 'Date'. Simply Copy / Paste your output to a non-unicode console produces.

Index([u'?Date', u'Open', u'High', u'Low', u'Close', u'Volume'], dtype='object')