如何用Pandas DataFrame中的以前的值替换NaN? [英] How to replace NaNs by preceding values in pandas DataFrame?

查看:330
本文介绍了如何用Pandas DataFrame中的以前的值替换NaN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个DataFrame,一些 NaN s:

Suppose I have a DataFrame with some NaNs:

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]])
>>> df
    0   1   2
0   1   2   3
1   4 NaN NaN
2 NaN NaN   9

我需要做的是用第一个非 NaN NaN c>在上面的同一列中的值。假设第一行将永远不会包含 NaN 。所以对于上一个例子,结果将是

What I need to do is replace every NaN with the first non-NaN value in the same column above it. It is assumed that the first row will never contain a NaN. So for the previous example the result would be

   0  1  2
0  1  2  3
1  4  2  3
2  4  2  9

我可以循环遍历整个DataFrame列-column,逐个元素,并直接设置值,但是有没有一个简单的(最佳的无循环)方法来实现这个?

I can just loop through the whole DataFrame column-by-column, element-by-element and set the values directly, but is there an easy (optimally a loop-free) way of achieving this?

推荐答案

您可以使用 fillna 方法,并将方法指定为 ffill (forward fill):

You could use the fillna method on the DataFrame and specify the method as ffill (forward fill):

>>> df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]])
>>> df.fillna(method='ffill')
   0  1  2
0  1  2  3
1  4  2  3
2  4  2  9

此方法...


传播]最后一次有效观察转到下一个有效

propagate[s] last valid observation forward to next valid

要相反的方法,还有一个 bfill 方法。

To go the opposite way, there's also a bfill method.

此方法不会修改DataFrame,您需要将返回的DataFrame重新绑定到变量,否则请指定 inplace = True

This method doesn't modify the DataFrame inplace - you'll need to rebind the returned DataFrame to a variable or else specify inplace=True:

df.fillna(method='ffill', inplace=True)

这篇关于如何用Pandas DataFrame中的以前的值替换NaN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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