我已经设法使用StackOverflow解决了许多问题,但这是我第一次遇到在其他地方找不到的问题,我自己也无法解决。。。
我在jupyter笔记本上使用熊猫数据框架,其中包含亚马逊产品的文本评论和分数。以下是我的代码:
import pandas as pd
data = pd.read_csv("AmazonSampleForStudentOffice.csv")
reviews = data[['reviewText', 'score', 'len_text']]
reviews.head(5)
这就是结果:
reviewText score len_text
0 Wow! Do I consider myself lucky! I got this CX... 5 274
1 The Optima 45 Electric Stapler has a sleek mod... 5 108
2 This tape does just what it's supposed to.And ... 5 18
3 It is rare that I look for a more expensive pr... 5 104
4 I know of no printer that makes such great pri... 5 34
并且对数据帧进行切片可以很好地工作:
reviews[0:2]
reviewText score len_text
0 Wow! Do I consider myself lucky! I got this CX... 5 274
1 The Optima 45 Electric Stapler has a sleek mod... 5 108
然而,如果我想选择一行,jupyter会在所选索引上抛出一个Key错误:
reviews[0]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
c:usersrobinappdatalocalprogramspythonpython38-32libsite-packagespandascoreindexesbase.py in get_loc(self, key, method, tolerance)
2896 try:
-> 2897 return self._engine.get_loc(key)
2898 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 0
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-7-a635d1333a53> in <module>
----> 1 reviews[0]
c:usersrobinappdatalocalprogramspythonpython38-32libsite-packagespandascoreframe.py in __getitem__(self, key)
2993 if self.columns.nlevels > 1:
2994 return self._getitem_multilevel(key)
-> 2995 indexer = self.columns.get_loc(key)
2996 if is_integer(indexer):
2997 indexer = [indexer]
c:usersrobinappdatalocalprogramspythonpython38-32libsite-packagespandascoreindexesbase.py in get_loc(self, key, method, tolerance)
2897 return self._engine.get_loc(key)
2898 except KeyError:
-> 2899 return self._engine.get_loc(self._maybe_cast_indexer(key))
2900 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2901 if indexer.ndim > 1 or indexer.size > 1:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 0
有人知道是什么导致了这个问题吗?我觉得很奇怪,切片工作得很好,但选择一个索引会抛出一个错误。。。
正如您所看到的,我尝试了不同的方法来从数据帧中选择某些行,它们都很好。我也试着重新安装熊猫和jupyter笔记本,但它仍然抛出错误。。。
提前感谢!
单独的索引运算符(如reviews[]
(只能通过布尔表达式选择行,例如使用像reviews[:2]
这样的切片(您的0已过时(,或像reviews['score']
那样选择列。如果要按位置进行索引,则需要.ilog属性,就像reviews.iloc[0, :]
中一样,它只提供第一行,但提供所有列。
如果您想了解panda索引,请关注.loc和.iloc属性,它们都可以在二维中工作。单独的索引运算符只能用于在一维中进行选择,并且有相当多的限制。