带KeyError的简单循环:8167

2024-07-08 03:26:00 发布

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

我试图解析一列分号分隔的单元格。该列如下所示:

Operations
ANALYSIS; LABEL; MANUFACTURE; PACK; STERILIZE
ANALYSIS; LABEL; PACK; STERILIZE
API MANUFACTURE

现在我编写一个简单的循环,将此列解析为唯一列表:

a=list()
for i in range(9552):
    a+=ops_list[i].split(";")

熊猫吐出如下错误:

    ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-208-aab8f3fc6761> in <module>
      1 a=list()
      2 for i in range(9552):
----> 3     a+=ops_list[i].split(";")

/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    866         key = com.apply_if_callable(key, self)
    867         try:
--> 868             result = self.index.get_value(self, key)
    869 
    870             if not is_scalar(result):

/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4318         try:
   4319             return self._engine.get_value(s, k,
-> 4320                                           tz=getattr(series.dtype, 'tz', None))
   4321         except KeyError as e1:
   4322             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 8167

但是,如果我只是在每个循环中打印,例如:

a=list()
for i in range(9552):
    print(ops_list[i].split(";"))

它起作用了。你知道吗


Tags: keyinselfpandasforgetindexvalue

热门问题