Home > OS >  Python Pandas - Fill in df NaN values
Python Pandas - Fill in df NaN values

Time:04-17

The dataframe has NaN values. These cells can be filled in with the first actual value in the cells above them. Is this possible? To fill in these values while iterating the rows and say, if cell is NaN, use the above cell value, until next cell down has anything but NaN, then stop filling in and move on until another NaN is encountered.

The results here would be the column df['Line'] would have ['A', 'A', 'A', 'B'] and df['Generation'] would have ['2020A', '2020B', '2020B', '2020A'].

Line Generation SNP-1 SNP-2 SNP-3
A 2020A A A A
NaN 2020B A A A
NaN NaN A A A
B 2020A A A A

CodePudding user response:

The docs here show how to do it:

df.fillna(method="ffill")
  • Related