IndexingError:索引器过多

时间:2019-06-10 00:07:15

标签: python dataframe indexing

我创建了一个函数来执行许多操作,其中之一是将数据帧的每一行的值存储在变量valueCol中,但是,当我运行它时,它会显示索引过多的错误。为了遍历各列,我使用了for循环。 我要提取值的数据框是:

    val01_ambient_temperature   val01_ambient_winddir   val01_ambient_windspeed
measure_time            
2019-01-01 00:00:00 10.75   54.699997   9.20
2019-01-01 00:10:00 10.00   54.810001   9.50
2019-01-01 00:20:00 10.00   53.139999   8.50
2019-01-01 00:30:00 10.00   54.400002   8.87
2019-01-01 00:40:00 10.00   50.920002   9.25

我尝试用一​​列模型,删除了for循环,它起作用了。问题是当我在iloc中引入计数器以标识要存储的列时。

功能

def autoArimaModelVar(dataModel,dataTotal,dataTotalCopy,counter1):
...
    valueCol=pd.DataFrame(dataTotalCopy.iloc[:,counter1]) # -----XXXX--- problem here with counter


调用该函数

dataImport_selVar_copy=dataImport_selVar # not to modify original data
counterTest=0

for column in dataImport_selVar100:
    if counterTest==0: #initialize
        result01=autoArimaModelVar(dataImport_selVar100[column],dataImport_selVar[column],
                             dataImport_selVar_copy[column],counterTest)
    else:


result11=autoArimaModelVar(dataImport_selVar100[column],dataImport_selVar[column], dataImport_selVar_copy[column],counterTest)
result01=result01.append(result11,sort=False) #print only 18

    counterTest +=1

我希望附加valueCol,这样就可以了:


measure_time      value     signal          
2019-01-01 00:00:00 10.75   val01_ambient_temperature
2019-01-01 00:10:00 10.00   val01_ambient_temperature
2019-01-01 00:20:00 10.00   val01_ambient_temperature
2019-01-01 00:30:00 10.00   val01_ambient_temperature
2019-01-01 00:40:00 10.00   val01_ambient_temperature            
2019-01-01 00:00:00 54.699997  val01_ambient_winddir
2019-01-01 00:10:00 54.810001  val01_ambient_winddir
2019-01-01 00:20:00 53.139999  val01_ambient_winddir
2019-01-01 00:30:00 54.400002  val01_ambient_winddir
2019-01-01 00:40:00 50.920002  val01_ambient_winddir          
2019-01-01 00:00:00 9.20  val01_ambient_windspeed
2019-01-01 00:10:00 9.50  val01_ambient_windspeed
2019-01-01 00:20:00 8.50  val01_ambient_windspeed
2019-01-01 00:30:00 8.87  val01_ambient_windspeed
2019-01-01 00:40:00 9.25  val01_ambient_windspeed

但是我遇到以下错误:

---------------------------------------------------------------------------
IndexingError                             Traceback (most recent call last)
<ipython-input-294-8229f0a1b81e> in <module>
      9     if counterTest==0: #inicialize
     10         result01=autoArimaModelVar(dataImport_selVar100[column],dataImport_selVar[column],
---> 11                              dataImport_selVar_copy[column],counterTest)
     12     else:
     13         result11=autoArimaModelVar(dataImport_selVar100[column],dataImport_selVar[column],

<ipython-input-293-965350b41f10> in autoArimaModelVar(dataModel, dataTotal, dataTotalCopy, counter1)
     40 
     41     # Create a column called value with the value of the measures
---> 42     valueCol=pd.DataFrame(dataTotalCopy.iloc[:,counter1]) # -----XXXX--- problem here with counter
     43 
     44     # Concatenate using the index, the values and the forecast

~/anaconda3/lib/python3.7/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

~/anaconda3/lib/python3.7/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)

~/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in _has_valid_tuple(self, key)
    218         for i, k in enumerate(key):
    219             if i >= self.obj.ndim:
--> 220                 raise IndexingError('Too many indexers')
    221             try:
    222                 self._validate_key(k, i)

IndexingError: Too many indexers

1 个答案:

答案 0 :(得分:0)

已解决,由于它是for循环的一部分,因此已被索引,因此您不应再次对其进行索引。

valueCol=dataTotalCopy