索引Pandas数据框时出现KeyError [英] KeyError when indexing Pandas dataframe

查看:2176
本文介绍了索引Pandas数据框时出现KeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将csv文件中的数据读取到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']

产生以下结果

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

推荐答案

您很可能在文件的开头有一个额外的字符,该字符位于您的第一列名称'Date'之前.只需将输出复制/粘贴到非Unicode控制台生成的文件即可.

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')

这篇关于索引Pandas数据框时出现KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆