I have a dataframe where I would like to fill null values in a dataframe following two conditions
Condition 1: value after NaN (in this example 10)
> value before NaN (7.5)
2.75
7.5
NaN
NaN
NaN
10
Equally increments from 7.5 to 10. So it would be like
2.75
7.5
8.125
8.75
9.375
10
PS: The increment is calculated as following (10-7.5/4) = 0.625
Condition 2: Value after NaN is < = value before Nan
2.75
10
NaN
NaN
NaN
7.5
Forward fill NaN values
2.75
10
10
10
10
7.5
If you are targeting this to do for a series, you can use:
df['Col'].fillna(df['Col'].interpolate().cummax())
For a dataframe:
df.fillna(df.interpolate().cummax())
Showing for a dataframe for con1 and cond 2( can be replicated for a series too )
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.