[英]Pandas Fill consecutive null values with range of values between the first non null value before and after
我有一个 dataframe,我想在以下两个条件下在 dataframe 中填充 null 值
条件 1: NaN 之后的值(in this example 10)
> NaN 之前的值(7.5)
2.75
7.5
NaN
NaN
NaN
10
从 7.5 到 10 同样递增。所以就像
2.75
7.5
8.125
8.75
9.375
10
PS:增量计算如下(10-7.5/4) = 0.625
条件2: NaN之后的值<=Nan之前的值
2.75
10
NaN
NaN
NaN
7.5
前向填充 NaN 值
2.75
10
10
10
10
7.5
如果您的目标是为系列做这件事,您可以使用:
df['Col'].fillna(df['Col'].interpolate().cummax())
对于 dataframe:
df.fillna(df.interpolate().cummax())
为 con1 和 cond 2 显示 dataframe(也可以为系列复制)
print(df1)
Col Col1
0 2.75 2.75
1 7.50 10.00
2 NaN NaN
3 NaN NaN
4 NaN NaN
5 10.00 7.50
print(df1.fillna(df1.interpolate().cummax()))
Col Col1
0 2.750 2.75
1 7.500 10.00
2 8.125 10.00
3 8.750 10.00
4 9.375 10.00
5 10.000 7.50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.