Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyError when indexing Pandas dataframe

Tags:

python

pandas

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

import pandas as pd 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

like image 495
Simbi Avatar asked May 19 '14 07:05

Simbi


People also ask

How do I fix KeyError in Python?

The Usual Solution: . If the KeyError is raised from a failed dictionary key lookup in your own code, you can use . get() to return either the value found at the specified key or a default value.

Why is pandas not recognizing column name?

Typically this error occurs when you simply misspell a column names or include an accidental space before or after the column name.

How do I reindex a DataFrame in Python?

One can reindex a single column or multiple columns by using reindex() method and by specifying the axis we want to reindex. Default values in the new index that are not present in the dataframe are assigned NaN.


2 Answers

As mentioned by alko, it is probably extra character at the beginning of your file. When using read_csv, you can specify encoding to deal with encoding and heading character, known as BOM (Byte order mark)

df = pd.read_csv('values.csv', delimiter=',', encoding="utf-8-sig") 

This question finds some echoes on Stackoverflow: Pandas seems to ignore first column name when reading tab-delimited data, gives KeyError

like image 64
Guillaume Jacquenot Avatar answered Sep 21 '22 21:09

Guillaume Jacquenot


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') 
like image 27
alko Avatar answered Sep 22 '22 21:09

alko