索引错误:索引器太多

2024-09-29 07:25:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个函数来做很多事情,其中一个是将数据帧的每一行的值存储在variable 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中引入计数器来标识我存储的列时。在

功能

^{pr2}$

调用函数

^{3}$

我想把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

Tags: thekeyinselfcolumntemperaturetuplegetitem
1条回答
网友
1楼 · 发布于 2024-09-29 07:25:39

已解决,因为它是for循环的一部分,因此它已被索引,因此不应再次索引它。在

valueCol=dataTotalCopy

相关问题 更多 >