weixin_44822446
2019-11-13 12:21 阅读 978

帮帮忙吧,急哭了:python实现拉格朗日插值进行缺失值插补时报错,出现Keyerror:0问题?

图片说明

数据截图

import pandas as pd
from scipy.interpolate import lagrange
inputfile = './test1.xlsx'
outputfile = './test1_1.xlsx'
def ployinterp_column(s,n,k=6):
    y = s[list(range(n-k,n)) + list(range(n+1,n+1-k))]
    y = y[y.notnull()]
    return lagrange(y.index,list(y))(n)
for i in data.columns:
    for j in range(len(data)):
        if (data[i].isnull())[j]:
            data[i][j] = ployinterp_column(data[i],j)

报错

KeyError                                  Traceback (most recent call last)
<ipython-input-28-02cf873cddff> in <module>()
      1 for i in data.columns:
      2     for j in range(len(data)):
----> 3         if (data[i].isnull())[j]:
      4             if (data[i].isnull())[j] in data:
      5                 data[i][j] = ployinterp_column(data[i],j)

C:\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    765         key = com._apply_if_callable(key, self)
    766         try:
--> 767             result = self.index.get_value(self, key)
    768 
    769             if not is_scalar(result):

C:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   3116         try:
   3117             return self._engine.get_value(s, k,
-> 3118                                           tz=getattr(series.dtype, 'tz', None))
   3119         except KeyError as e1:
   3120             if len(self) > 0 and self.inferred_type in ['integer', '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: 0

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

相关推荐