2016-08-15 46 views
0

當然我缺少明顯的東西 - 但我對這個結果百思不得其解:蟒蛇2.7大熊貓Dataframe.from_csv日期列無法訪問

環境:

的Ubuntu 16.04.1 LTS 的Python 2.7.12 大熊貓0.18.1

CSV文件:

Date,Open,High,Low,Close,Volume 
12-Aug-16,107.78,108.44,107.78,108.18,18660434 
11-Aug-16,108.52,108.93,107.85,107.93,27484506 
10-Aug-16,108.71,108.90,107.76,108.00,24008505 

代碼:

import pandas as pd 

aapl = pd.DataFrame.from_csv('aapl.csv',index_col=None) 

print aapl.columns 

print aapl.Low.dtype 
print aapl['Low'].dtype 

# Fails - KeyError 
print aapl['Date'].dtype 

輸出:

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

的奧祕我是「日期」出現在列名單,但我不能滿足列。我錯過了什麼?

+2

我打賭你的文件是用BOM編碼的UTF8 – Boud

+0

我想你是的,我不確定編碼是什麼,但是肯定有特殊的字符被埋沒在那裏print repr(aapl.columns [0]) results in'\ xef \ xbb \ xbfDate'。 –

回答

0

爲了解決這個問題,@Boud回答了這個問題。用repr()打印列,即打印(repr(aapl.columns [0])顯示密鑰字符串中的編碼字符,以防止它被發現。