Backward and forward filling

NaN values can be filled based on the last known values. To understand this, let's consider taking our store dataframe as an example.

We want to fill store4 using the forward-filling technique:

dfx.store4.fillna(method='ffill')

And the output of the preceding code is the following:

apple 20.0
banana 20.0
kiwi 20.0
grapes 20.0
mango 20.0
watermelon 18.0
oranges 18.0
Name: store4, dtype: float64

Here, from the forward-filling technique, the last known value is 20 and hence the rest of the NaN values are replaced by it. 

The direction of the fill can be changed by changing method='bfill'Check the following example:

dfx.store4.fillna(method='bfill')

And the output of the preceding code is as follows:

apple 20.0
banana 18.0
kiwi 18.0
grapes 18.0
mango 18.0
watermelon 18.0
oranges NaN
Name: store4, dtype: float64

Note here that the NaN values are replaced by 18.0

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.69.157