Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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']

like image 593
daniel lee Avatar asked Dec 30 '25 12:12

daniel lee


1 Answers

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
like image 69
รยקคгรђשค Avatar answered Jan 01 '26 04:01

รยקคгรђשค



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!