'Replacing a value in a column with a value from the same column based upon information

I am looking for a way to do Missing value imputation. There is a table of entries over a given time, with an entry per hour done on days. There is a seperate entry per hour, which also has a day. So the table would be:

Day Hour entry
01.01.1998 1 78
01.01.1998 2 87
01.01.1998 3 NaN
... ... ...
31.01.1998 23 784
31.01.1998 24 8734

Now I want to detect certain NaN values in my column of entry, and replace that with the entry done the day before. (Or in case its the first day, from the day after.) How would I do that?



Solution 1:[1]

You can do

df['entry'] = df['entry'].ffill().bfill()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ynjxsjmh