Python 2D矩阵插值

时间:2018-10-24 11:51:32

标签: python pandas

我有一个包含一些Float值和许多NaN值的矩阵。现在我想对NaN值进行插值。

矩阵如下:

[
  [NaN, NaN, 3.9, NaN, NaN],
  [3.2, NaN, NaN, 2.9, NaN],
  [NaN, NaN, 2.5, NaN, NaN],
  [7.6, NaN, NaN, NaN, 1.3],
  [NaN, 0.6, NaN, NaN, NaN]
]

我知道存在 scipy.interpolate.interp2d https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.interp2d.html)。但是我想进行样条插值。使用 pandas https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.interpolate.html),我可以制作样条曲线,但是我不确定它是否适用于二维数据。也许有人可以帮助我! :-)

谢谢

1 个答案:

答案 0 :(得分:0)

每年,也许您需要这是熊猫解决方案:

a = [
  [None, None, 3.9, None, None],
  [3.2, None, None, 2.9, None],
  [None, None, 2.5, None, None],
  [7.6, None, None, None, 1.3],
  [None, 0.6, None, None, None]
]
b = {i:a[i] for i in range(len(a))}
c = pd.DataFrame(b).T
c.interpolate(limit_direction ='both',inplace=True,axis=0)

如果您需要逐行进行插值填充,请切换axis=1