带有KeyError的熊猫简单循环:8167

时间:2019-01-27 03:36:39

标签: python pandas series

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

@bot.command()
async def repeat(times: int, *,content="Repeating..."):
  for i in range(times):
    if times > 10:
      await bot.say("Cannot spam more than 10 messages at a time.")
      return
    else:
      await bot.say(content)

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

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

有效。

2 个答案:

答案 0 :(得分:0)

您应使用EmployeeID LastName ReportsTo level ----------- -------------------- ----------- ----------- 2 Fuller NULL 0 --anchor call --now 'Managers' CTE is the anchor (level 0) 1 Davolio 2 1 3 Leverling 2 1 4 Peacock 2 1 5 Buchanan 2 1 8 Callahan 2 1 --now 'Managers' CTE is resultset level 1 6 Suyama 5 2 7 King 5 2 9 Dodsworth 5 2

tolist

答案 1 :(得分:0)

结果是需要重新索引输入数据帧。我刚刚更改了索引号,现在它可以工作了.....