python给出“ IndexError:单个位置索引器超出范围”

时间:2018-10-16 15:33:12

标签: python pandas jupyter data-science data-analysis

下面是我使用的代码:

#Divide data into train and test
p_dt_train, p_dt_test = train_test_split(p_main_df, test_size=0.2)

#Shape #(3485868, 26)
p_dt_train.shape

p_fit_DT = DecisionTreeRegressor(max_depth=2).fit(p_dt_train.iloc[:,0:26], p_dt_train.iloc[:,26])

但是当我运行上面的代码行p_fit_DT时,会发生以下错误:

IndexError                                Traceback (most recent call last)
<ipython-input-59-9bbf02d21cd5> in <module>()
      1 # In[1]:
      2 #Decision tree for regression
----> 3 p_fit_DT = DecisionTreeRegressor(max_depth=2).fit(p_dt_train.iloc[:,0:26], p_dt_train.iloc[:,26])
      4 
      5 #Apply model on test data

D:\My_Work\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1470             except (KeyError, IndexError):
   1471                 pass
-> 1472             return self._getitem_tuple(key)
   1473         else:
   1474             # we by definition only have the 0th axis

D:\My_Work\anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup)
   2011     def _getitem_tuple(self, tup):
   2012 
-> 2013         self._has_valid_tuple(tup)
   2014         try:
   2015             return self._getitem_lowerdim(tup)

D:\My_Work\anaconda3\lib\site-packages\pandas\core\indexing.py in _has_valid_tuple(self, key)
    220                 raise IndexingError('Too many indexers')
    221             try:
--> 222                 self._validate_key(k, i)
    223             except ValueError:
    224                 raise ValueError("Location based indexing can only have "

D:\My_Work\anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis)
   1955             return
   1956         elif is_integer(key):
-> 1957             self._validate_integer(key, axis)
   1958         elif isinstance(key, tuple):
   1959             # a tuple should already have been caught by this point

D:\My_Work\anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_integer(self, key, axis)
   2007         l = len(ax)
   2008         if key >= l or key < -l:
-> 2009             raise IndexError("single positional indexer is out-of-bounds")
   2010 
   2011     def _getitem_tuple(self, tup):

IndexError: single positional indexer is out-of-bounds

请指导我要去哪里了。 预先感谢。

1 个答案:

答案 0 :(得分:0)

如果您的var express = require('express'); var proxy = require('http-proxy-middleware'); var app = express(); app.use('/api', proxy({target: 'http://www.example.org', changeOrigin: true})); app.listen(3000); // http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar 的形状为DataFrame,则沿轴1的索引的范围为0到25(含0和25)。因此,也许您打算这样做:

(3485868, 26)

如果将代码重构为更多步骤,则可能会更加清晰,例如:

p_fit_DT = \
DecisionTreeRegressor(max_depth=2).fit(p_dt_train.iloc[:,0:25], 
p_dt_train.iloc[:,25])