Skip to content
Advertisement

Pandas DataFrame – Filling a repeating null value with a preceding none-null value

How do I fill repeating null values with a preceding non-null value?

df = pd.DataFrame(['a','a', None, 'b', None, None])

For instance, the data frame above would be populated as:

[‘a’,’a’,’a’,’b’,’b’,’b’]

Advertisement

Answer

Use df.fillna with the ffill method, as follows:

df.fillna(method='ffill')

Example:

>>> df = pd.DataFrame(['a','a', None, 'b', None, None])
>>> df.fillna(method='ffill')
   0
0  a
1  a
2  a
3  b
4  b
5  b
Advertisement