The ffill()
method in pandas is used to forward fill (or propagate the last valid value forward) missing values in a DataFrame. This means that it fills the missing values with the last valid value that occurred before it.
To use the ffill()
method on a pandas DataFrame, you can follow these steps:
main.py20 chars2 lines
main.py103 chars3 lines
ffill()
method to fill the missing values:main.py23 chars2 lines
The df_filled
DataFrame will now have the missing values filled with the last valid values. In the example above, the missing value in Column1
will be filled with the value 2, while the missing values in Column2
will be filled with the value 5.
It's worth noting that the ffill()
method propagates the last valid value forward, so if the first value in a column is missing, it will remain as missing.
Tags: pandas, fillna
gistlibby LogSnag