Python 在对现有值筛选数据帧时,位置索引器超出范围
我正在研究Kaggle上多类分类的初学者问题: 此处提供的数据: 我的anaconda刚刚更新到最新版本(pyton3,windows10安装程序) 然后我想用这个简单的循环检查特征之间的相关性:Python 在对现有值筛选数据帧时,位置索引器超出范围,python,pandas,Python,Pandas,我正在研究Kaggle上多类分类的初学者问题: 此处提供的数据: 我的anaconda刚刚更新到最新版本(pyton3,windows10安装程序) 然后我想用这个简单的循环检查特征之间的相关性: ## check numeric features: columns 10:-1 plt.rcParams["figure.figsize"] = (10,8) covers = list(train.Cover_Type.unique()) covers.sort() for cover_typ
## check numeric features: columns 10:-1
plt.rcParams["figure.figsize"] = (10,8)
covers = list(train.Cover_Type.unique())
covers.sort()
for cover_type in covers:
print(f'Cover Type: {cover_type}\n')
mask = train[train['Cover_Type']==cover_type].index
sns.heatmap(train.iloc[mask,:10].corr(),annot=True)
plt.show();
它工作正常,直到我点击Cover\u Type 3,然后它抛出一个错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-72-c559c9b2bf10> in <module>
9
10 plt.figure(figsize=(10,8))
---> 11 sns.heatmap(train.iloc[mask,:10].corr(),annot=True)
12 plt.show();
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
1492 except (KeyError, IndexError, AttributeError):
1493 pass
-> 1494 return self._getitem_tuple(key)
1495 else:
1496 # we by definition only have the 0th axis
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup)
2141 def _getitem_tuple(self, tup):
2142
-> 2143 self._has_valid_tuple(tup)
2144 try:
2145 return self._getitem_lowerdim(tup)
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _has_valid_tuple(self, key)
221 raise IndexingError('Too many indexers')
222 try:
--> 223 self._validate_key(k, i)
224 except ValueError:
225 raise ValueError("Location based indexing can only have "
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis)
2079
2080 if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis):
-> 2081 raise IndexError("positional indexers are out-of-bounds")
2082 else:
2083 raise ValueError("Can only index by location with "
IndexError: positional indexers are out-of-bounds
<Figure size 720x576 with 0 Axes>
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在里面
9
10 plt.图(图尺寸=(10,8))
--->11 sns.heatmap(train.iloc[mask,:10].corr(),annot=True)
12 plt.show();
~\Anaconda3\lib\site packages\pandas\core\index.py in\uuuuu getitem\uuuuuu(self,key)
1492除外(键错误、索引器错误、属性错误):
1493通行证
->1494返回self.\u getitem\u元组(键)
1495其他:
1496#根据定义,我们只有第0轴
~\Anaconda3\lib\site packages\pandas\core\index.py在\u getitem\u元组(self,tup)中
2141 def_getitem_元组(self,tup):
2142
->2143 self.\u具有有效的\u元组(tup)
2144尝试:
2145返回自我。\u getitem\u lowerdim(tup)
~\Anaconda3\lib\site packages\pandas\core\index.py in\u具有有效的\u元组(self,key)
221 raise IndexingError('索引器太多')
222尝试:
-->223自我验证密钥(k,i)
224除值错误外:
225 raise VALUE ERROR(“基于位置的索引只能有”
~\Anaconda3\lib\site packages\pandas\core\index.py in\u validate\u key(self、key、axis)
2079
2080如果len(arr)和(arr.max()>=len\u轴或arr.min()<-len\u轴):
->2081提升索引器(“位置索引器超出范围”)
2082其他:
2083 raise VALUE ERROR(“只能按具有的位置进行索引”)
索引器:位置索引器超出范围
为什么会出现这种情况?当您实际想要使用
loc
时,使用iloc
时可能会发生这种情况。第一种基于整数位置,第二种基于标签。您是否检查过这里是否存在这种情况?为什么封面类型1,2,4,5,6,7使用.iloc[]设置正确的索引?好的,我用你的数据运行了代码,得到了预期的结果。所以这似乎是版本问题。我在colab.research.google.com上运行了代码,它有python 3.6.8、pandas 0.24.2、numpy 1.16.5和seaborn 0.9.0。
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-72-c559c9b2bf10> in <module>
9
10 plt.figure(figsize=(10,8))
---> 11 sns.heatmap(train.iloc[mask,:10].corr(),annot=True)
12 plt.show();
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
1492 except (KeyError, IndexError, AttributeError):
1493 pass
-> 1494 return self._getitem_tuple(key)
1495 else:
1496 # we by definition only have the 0th axis
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup)
2141 def _getitem_tuple(self, tup):
2142
-> 2143 self._has_valid_tuple(tup)
2144 try:
2145 return self._getitem_lowerdim(tup)
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _has_valid_tuple(self, key)
221 raise IndexingError('Too many indexers')
222 try:
--> 223 self._validate_key(k, i)
224 except ValueError:
225 raise ValueError("Location based indexing can only have "
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis)
2079
2080 if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis):
-> 2081 raise IndexError("positional indexers are out-of-bounds")
2082 else:
2083 raise ValueError("Can only index by location with "
IndexError: positional indexers are out-of-bounds
<Figure size 720x576 with 0 Axes>