如何用行中的上一个填充nan值? [英] How to fill nan value with the previous available in the row?

查看:238
本文介绍了如何用行中的上一个填充nan值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于数据是时间序列,因此我需要通过使用一行中的先前可用值来填充数据帧中的nan值.这里有一个例子:

I need to fill the nan value in a dataframe by using the previous available value in a row, as the data are timeseries. Here there is an example:

1   2   3   4
b   b   nan c
c   nan d   nan
d   nan nan c

我需要的是:

1   2   3   4
b   b   b   c
c   c   d   d
d   d   d   c

我从panda那里知道方法fillna,但是这些方法仅基于列.我考虑过进行换位并使用fillna方法,但是我想知道是否有更有效的方法.

I know the method fillna from panda, but the methods are only based on columns. I think about doing a transposition and after the use of the fillna method, but I would like to know if there are more efficient way to do that.

推荐答案

在第一个轴上使用ffill.

df.ffill(axis=1)

   1  2  3  4
0  b  b  b  c
1  c  c  d  d
2  d  d  d  c

这篇关于如何用行中的上一个填充nan值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆