Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill blank cells in Pandas dataframe with value from cell above it

Consider a situation.

A    B    C
1    X    Park
2    Y    
3    Z    Team
4    L
5    M    Cycle
6    K    
7    N    

Expected output:

A    B    C
1    X    Park
2    Y    Park
3    Z    Team
4    L    Team
5    M    Cycle
6    K    Cycle
7    N    Cycle

There are millions of rows so can not be done manually. Gaps between empty cells and cells with values can be more than 1000s of rows in column C.

like image 346
Optimus Avatar asked Dec 18 '25 02:12

Optimus


1 Answers

You need Pandas ffill():

df.ffill()

See the pandas documentation for parameters: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.ffill.html

like image 64
scima96 Avatar answered Dec 20 '25 15:12

scima96