Pandas: Fill missing values using last available

荒凉一梦 提交于 2021-02-05 06:45:06

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/45320928/pandas-fill-missing-values-using-last-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!