适用于熊猫的KeyError

时间:2019-12-03 21:01:38

标签: python pandas

我正在将lambda函数应用于数据框中的选定列的行-

  metrics['change_in_traffic'] = metrics[[
      'current_position', 'traffic'
  ]].apply(
      lambda x: _change(x, weights_for_position),
      axis=1).reset_index(drop=True)

我正在_change函数中执行此操作-

  current_position, traffic = x
  new_position = current_position - _POSITION_CHANGE
  if (current_position > _POSITION_CHANGE) and (new_position in weights_for_position) and (
      current_position in weights_for_position):
    return (weights_for_position[new_position] - weights_for_position[current_position]) * query_traffic
  return 0

weights_for_position是一个字典。

尽管我正在检查dict中的current_position和new_position,但是我在返回-KeyError: (679, 'occurred at index 36967')时遇到了KeyError。我检查了Apply的问题,但是大多数解决方案都说add axis = 1,我已经在做了。

任何想法可能出什么问题吗?

0 个答案:

没有答案