Home > Net >  Fill it with the value before that NaN value - Pandas
Fill it with the value before that NaN value - Pandas

Time:11-30

I have 10 row height data, where there is a NaN value and I want to fill it with a value before that NaN value.

How can I implement this in python language especially pandas library, and here is my dataframe.

        Datetime    Height
0   2011-03-11 00:00:00 3503.3519
1   2011-03-11 00:00:15 3503.3529
2   2011-03-11 00:00:30 3503.3529
3   2011-03-11 00:00:45 3503.3519
4   2011-03-11 00:01:00 NaN
5   2011-03-11 00:01:15 3503.3519
6   2011-03-11 00:01:30 3503.3529
7   2011-03-11 00:01:45 3503.3539
8   2011-03-11 00:02:00 3503.3550
9   2011-03-11 00:02:15 3503.3550
Height = df['Height']
Height[4] = Height[4-1]
Print(Height[4])

3503.3519

Where the results I want are as follows:

        Datetime    Height
0   2011-03-11 00:00:00 3503.3519
1   2011-03-11 00:00:15 3503.3529
2   2011-03-11 00:00:30 3503.3529
3   2011-03-11 00:00:45 3503.3519
4   2011-03-11 00:01:00 3503.3519
5   2011-03-11 00:01:15 3503.3519
6   2011-03-11 00:01:30 3503.3529
7   2011-03-11 00:01:45 3503.3539
8   2011-03-11 00:02:00 3503.3550
9   2011-03-11 00:02:15 3503.3550

CodePudding user response:

Try with ffill

df['Height'] = df['Height'].ffill()
  • Related