Kelaref
Kelaref

Reputation: 517

Pandas: Fill missing values using last available

I have a dataframe as follows:

              A       B 
  zDate
01-JAN-17    100     200
02-JAN-17    111     203
03-JAN-17    NaN     202
04-JAN-17    109     205
05-JAN-17    101     211
06-JAN-17    105     NaN
07-JAN-17    104     NaN

What is the best way, to fill the missing values, using last available ones?

Following is the intended result:

              A       B 
  zDate
01-JAN-17    100     200
02-JAN-17    111     203
03-JAN-17    111     202
04-JAN-17    109     205
05-JAN-17    101     211
06-JAN-17    105     211
07-JAN-17    104     211

Upvotes: 3

Views: 1711

Answers (1)

jezrael
jezrael

Reputation: 863531

Use ffill function, what is same as fillna with method ffill:

df = df.ffill()
print (df)
               A      B
zDate                  
01-JAN-17  100.0  200.0
02-JAN-17  111.0  203.0
03-JAN-17  111.0  202.0
04-JAN-17  109.0  205.0
05-JAN-17  101.0  211.0
06-JAN-17  105.0  211.0
07-JAN-17  104.0  211.0

Upvotes: 6

Related Questions