Python 索引 Pandas 数据帧时出现 KeyError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23731564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
KeyError when indexing Pandas dataframe
提问by Simbi
I am trying to read data from a csv file into a pandas dataframe, and access the first column 'Date'
我正在尝试将 csv 文件中的数据读取到 Pandas 数据框中,并访问第一列“日期”
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
如果我尝试访问任何其他列,如“打开”或“音量”,它会按预期工作
采纳答案by alko
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.
您很可能在文件的开头有一个额外的字符,该字符位于您的第一个列名之前'Date'
。只需将您的输出复制/粘贴到非 unicode 控制台生成。
Index([u'?Date', u'Open', u'High', u'Low', u'Close', u'Volume'], dtype='object')
回答by Guillaume Jacquenot
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)
正如 alko 所提到的,它可能是文件开头的额外字符。使用时read_csv
,可以指定encoding
处理编码和标题字符,称为BOM(字节顺序标记)
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
这个问题在 Stackoverflow 上找到了一些回应: Pandas 在读取制表符分隔的数据时似乎忽略了第一列名称,给出了 KeyError