熊猫:IndexError:使用迭代时,索引越界

时间:2019-09-12 20:22:45

标签: python python-3.x pandas dataframe

我遇到了一个奇怪的问题,我不知道到底是什么原因造成的。我正在尝试使用Iterrows遍历列的子集并根据当前列的行中的发现更新新的列。它似乎适用于我拥有的一个列表,但不适用于另一个列表。我已经将每个列表中的所有列都转换为字符串,但这似乎无法解决。

示例:

list1 = [item for item in list(df) if 'TR_' in item]

list2 = [item for item in list(df) if 'TS_' in item ]



for index, row in df[list1].iterrows():
   print(index, row)
   if pd.isna(row[index]):
      df.loc[index, 'new_col'] = 'good'
   else:
      df.loc[index, 'new_col'] = "bad"


for index, row in df[list2].iterrows():
   print(index, row)
   if pd.isna(row[index]):
      df.loc[index, 'new_col2'] = 'good'
   else:
      df.loc[index, 'new_col2'] = 'bad'

output:
  

KeyError跟踪(最近的呼叫   持续)   C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexes \ base.py   在get_value(self,series,key)3102中返回   self._engine.get_value(s,k,   -> 3103 tz = getattr(series.dtype,'tz',None))3104,除了KeyError   如e1:

     

pandas._libs.index.IndexEngine.get_value()中的pandas_libs \ index.pyx

     

pandas._libs.index.IndexEngine.get_value()中的pandas_libs \ index.pyx

     

pandas._libs.index.IndexEngine.get_loc()中的pandas_libs \ index.pyx

     

pandas_libs \ hashtable_class_helper.pxi在   pandas._libs.hashtable.PyObjectHashTable.get_item()

     

pandas_libs \ hashtable_class_helper.pxi在   pandas._libs.hashtable.PyObjectHashTable.get_item()

     

KeyError:3

     

在处理上述异常期间,发生了另一个异常:

     

IndexError Traceback(最近的呼叫   最后)在()        对于索引,在df [list2] .iterrows()中为17。        18打印(索引,行)   -> 19,如果pd.isna(row [index]):        20 df.loc [index,'new_col2'] ='兼容'        其他21个:

     

C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ series.py在    getitem ((自身,密钥)       764键= com._apply_if_callable(键,自)       765尝试:   -> 766结果= self.index.get_value(self,key)       767       768,如果不是is_scalar(result):

     

C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexes \ base.py   在get_value(self,series,key)3107 3108中尝试:   -> 3109返回libindex.get_value_box(s,key)3110,但IndexError:3111提高

     

pandas._libs.index.get_value_box()中的pandas_libs \ index.pyx

     

pandas._libs.index.get_value_box()中的pandas_libs \ index.pyx

     

IndexError:索引超出范围”

2 个答案:

答案 0 :(得分:1)

我指的是:

const inputs = document.querySelectorAll('thead input');
const ids = [... inputs].map(i => i.id);
console.log(ids);

即您使用 >>> df cat col1 col2 0 a 1 2 1 a 3 2 2 b 23 1 3 a 1 23 4 b 121 32 >>> for index, row in df.iterrows(): ... print(row[index]) ... a 3 1 Traceback (most recent call last): File "C:\Users\Grzegorz\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 4723, in get_value return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None)) File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value File "pandas\_libs\index.pyx", line 88, in pandas._libs.index.IndexEngine.get_value File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 3 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 2, in <module> File "C:\Users\Grzegorz\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\series.py", line 1064, in __getitem__ result = self.index.get_value(self, key) File "C:\Users\Grzegorz\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 4729, in get_value return libindex.get_value_box(s, key) File "pandas\_libs\index.pyx", line 51, in pandas._libs.index.get_value_box File "pandas\_libs\index.pyx", line 47, in pandas._libs.index.get_value_at File "pandas\_libs\util.pxd", line 98, in pandas._libs.util.get_value_at File "pandas\_libs\util.pxd", line 89, in pandas._libs.util.validate_indexer IndexError: index out of bounds 错误。您想在那里实现什么?

编辑

替换:

row[index]

使用

pd.isna(row[index])

这样做-您将:

(1)为给定的np.product(pd.isna(row)) 每列返回True / False的列表

row

(2)计算逻辑乘法(即pd.isna(row)),因此,仅当所有列均为a1 and a2 and ...True,否则na

False

答案 1 :(得分:0)

尝试重置列表前的索引。

 df=df.reset_index(drop=True)