【发布时间】:2015-09-18 17:43:52
【问题描述】:
尝试以 test_df["LABEL"][0] 的方式访问 pandas 数据框中的单个元素时出现错误。这是关于我如何加载数据的代码 sn-p:
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]
这是我得到的错误:
文件“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章 loc = self.get_loc(k) 文件“/usr/local/lib/python2.7/dist-packages/pandas/core/index.py”,行 第3619章 返回 super(Float64Index, self).get_loc(key, method=method) 文件 "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", 第 1572 行,在 get_loc 中 return 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) 关键错误:0.0
我错过了什么?
【问题讨论】:
-
对不起,你在这里尝试什么?如果你想访问第一行你可以做
test_df['LABEL'].iloc[0],很可能0不在你的索引中,请发布你原始输入数据的前几行 -
@EdChum 是的,这就是我想要做的。但令人惊讶的是,索引 train_df["LABEL"][0] 对我有用,但对 test_df 不起作用
-
顺便说一句,你不需要
test_df = pd.DataFrame(test_set),因为read_csv已经给你一个DataFrame