March 2020
Beginner to intermediate
352 pages
8h 40m
English
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.0banana 20.0kiwi 20.0grapes 20.0mango 20.0watermelon 18.0oranges 18.0Name: 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.0banana 18.0 ...
Read now
Unlock full access