benawad
benawad

Reputation: 705

Trouble accessing column in pandas dataframe

I created a DataFrame like so:

stock_data = pd.read_csv('http://www.google.com/finance/historical?output=csv&q=AAPL')

It has a Date column but when I call stock_data['Date'] I get a key error. How do I access the date for each row?

Upvotes: 2

Views: 109

Answers (1)

DSM
DSM

Reputation: 352959

It looks like some junk (in particular, a UTF-8 BOM) found its way into that column name:

In [16]: stock_data = pd.read_csv('http://www.google.com/finance/historical?output=csv&q=AAPL')

In [17]: stock_data.columns
Out[17]: Index([u'Date', u'Open', u'High', u'Low', u'Close', u'Volume'], dtype='object')

In [18]: stock_data.columns[0]
Out[18]: '\xef\xbb\xbfDate'

which is why it isn't working. One workaround:

In [19]: stock_data.columns = [col.decode("utf-8-sig") for col in stock_data.columns]

In [20]: stock_data.columns[0]
Out[20]: u'Date'

In [21]: stock_data["Date"].head()
Out[21]: 
0     4-Dec-14
1     3-Dec-14
2     2-Dec-14
3     1-Dec-14
4    28-Nov-14
Name: Date, dtype: object

Upvotes: 2

Related Questions