Python 访问熊猫数据帧索引时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31163495/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:34:53  来源:igfitidea点击:

error when accessing pandas dataframe index

pythonpandas

提问by Mohamed Ali JAMAOUI

I get an error when trying to access a single element in a pandas dataframe this way test_df["LABEL"][0]. Here is a code snippet on how I am loading the data:

尝试以这种方式 test_df["LABEL"][0] 访问 Pandas 数据帧中的单个元素时出现错误。这是关于我如何加载数据的代码片段:

print "reading test set"
test_set = pd.read_csv(data_path+"small_test_products.txt", header=0, delimiter="|")

print "shape of the test set", test_set.shape 
test_df = pd.DataFrame(test_set)
lengthOfTestSet = len(test_df["LABEL"])
print test_df["LABEL"][0]

Here is the error I am getting:

这是我得到的错误:

File "code.py", line 80, in print test_df["LABEL"][0] File "/usr/local/lib/python2.7/dist-packages/pandas/core/series.py", line 521, in getitemresult = self.index.get_value(self, key) File "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 3562, in get_value loc = self.get_loc(k) File "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 3619, in get_loc return super(Float64Index, self).get_loc(key, method=method) File "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 1572, in get_loc return self._engine.get_loc(_values_from_object(key)) File "pandas/index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas/index.c:3824) File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:3704) File "pandas/hashtable.pyx", line 541, in pandas.hashtable.Float64HashTable.get_item (pandas/hashtable.c:9914)
File "pandas/hashtable.pyx", line 547, in pandas.hashtable.Float64HashTable.get_item (pandas/hashtable.c:9852) KeyError: 0.0

文件“code.py”,第 80 行,打印 test_df[“LABEL”][0] 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/series.py”,第 521 行,在getitem中结果 = self.index.get_value(self, key) 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/index.py”,第 3562 行,在 get_value loc = self.get_loc(k ) 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/index.py”,第 3619 行,在 get_loc 中返回 super(Float64Index, self).get_loc(key, method=method) 文件“ /usr/local/lib/python2.7/dist-packages/pandas/core/index.py”,第 1572 行,在 get_loc 中返回 self._engine.get_loc(_values_from_object(key)) 文件“pandas/index.pyx”,第 134 行,pandas.index.IndexEngine.get_loc (pandas/index.c:3824) 文件“pandas/index.pyx”,第 154 行,pandas.index.IndexEngine.get_loc (pandas/index.c:3704) 文件“pandas/hashtable.pyx”,第 541 行,在 pandas.hashtable.Float64HashTable.get_item (pandas/hashtable.c:9914)
文件“pandas/hashtable.pyx”,第 547 行,在 pandas.hashtable.Float64HashTable.get_item (pandas/hashtable.c:9852) KeyError: 0.0

What am I missing?

我错过了什么?

采纳答案by Seth

Like EdChum said 0 is probably not in your index.

就像 EdChum 说的 0 可能不在您的索引中。

Try: df.iloc[0]or df['label'].iloc[0], which is integer based location.

尝试:df.iloc[0]or df['label'].iloc[0],这是基于整数的位置。

To reset the index if you are having trouble with that: df.reset_index(drop=True)

如果遇到问题,要重置索引: df.reset_index(drop=True)

Check out panda's indexing docfor more information on it

查看熊猫的索引文档以获取更多信息