在 pandas 中对DataFrame进行插值 [英] Interpolation on DataFrame in pandas
问题描述
我有一个DataFrame,比如说一个波动性表面,其索引为时间,而列为行权.如何进行二维插值?我可以reindex
,但是如何处理NaN
?我知道我们可以fillna(method='pad')
,但它甚至不是线性插值.有没有办法插入我们自己的方法进行插值?
I have a DataFrame, say a volatility surface with index as time and column as strike. How do I do two dimensional interpolation? I can reindex
but how do i deal with NaN
? I know we can fillna(method='pad')
but it is not even linear interpolation. Is there a way we can plug in our own method to do interpolation?
推荐答案
您可以使用DataFrame.interpolate
获得线性插值.
You can use DataFrame.interpolate
to get a linear interpolation.
In : df = pandas.DataFrame(numpy.random.randn(5,3), index=['a','c','d','e','g'])
In : df
Out:
0 1 2
a -1.987879 -2.028572 0.024493
c 2.092605 -1.429537 0.204811
d 0.767215 1.077814 0.565666
e -1.027733 1.330702 -0.490780
g -1.632493 0.938456 0.492695
In : df2 = df.reindex(['a','b','c','d','e','f','g'])
In : df2
Out:
0 1 2
a -1.987879 -2.028572 0.024493
b NaN NaN NaN
c 2.092605 -1.429537 0.204811
d 0.767215 1.077814 0.565666
e -1.027733 1.330702 -0.490780
f NaN NaN NaN
g -1.632493 0.938456 0.492695
In : df2.interpolate()
Out:
0 1 2
a -1.987879 -2.028572 0.024493
b 0.052363 -1.729055 0.114652
c 2.092605 -1.429537 0.204811
d 0.767215 1.077814 0.565666
e -1.027733 1.330702 -0.490780
f -1.330113 1.134579 0.000958
g -1.632493 0.938456 0.492695
对于更复杂的事情,您需要推出自己的函数来处理Series
对象,并根据需要填充NaN
值,然后返回另一个Series
对象.
For anything more complex, you need to roll-out your own function that will deal with a Series
object and fill NaN
values as you like and return another Series
object.
这篇关于在 pandas 中对DataFrame进行插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!