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
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.
Typically this error occurs when you simply misspell a column names or include an accidental space before or after the column name.
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.
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
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With