In pandas.fillna,
method : {‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default None
Method to use for filling holes in reindexed Series pad / ffill: propagate last valid observation forward to next valid backfill / bfill: use NEXT valid observation to fill gap
How can I fill values both backward and forward? None of the options seem to do this
It seems very simple and there may be a quicker way, but simply chaining the two, like so
df.fillna(method='ffill').fillna(method='bfill')
This will fill forwards first and then backwards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With