只想顺利毕业 2019-11-13 12:21 采纳率: 0%
浏览 1265

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

  • 写回答

1条回答 默认 最新

  • prqsdsg 2022-12-30 17:23
    关注

    转发:http://t.csdn.cn/novHk
    解决方案是1.将 y = s[list(range(n-k, n)) + list(range(n+1, n+1+k))]改为y = s.reindex(list(range(n-k, n)) + list(range(n+1, n+1+k)));2.将data[i][j] = ployinterp_column(data[i], j)改为data[i,j] = ployinterp_column(data[i], j)。

    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀