2016-08-15 121 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])显示密钥字符串中的编码字符,以防止它被发现。