帮帮忙吧,急哭了: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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问