如何用 Pandas DataFrame 中的前一个或下一个值替换 NaN? [英] How to replace NaNs by preceding or next values in pandas DataFrame?
问题描述
假设我有一个带有一些 NaN
的 DataFrame:
我需要做的是将每个 NaN
替换为其上方同一列中的第一个非 NaN
值.假设第一行永远不会包含 NaN
.所以对于前面的例子,结果是
<代码> 0 1 20 1 2 31 4 2 32 4 2 9
我可以逐列、逐个元素地遍历整个 DataFrame 并直接设置值,但是否有一种简单的(最好是无循环的)方法来实现这一点?
您可以使用 fillna
方法在 DataFrame 上并指定方法为 ffill
(前向填充):
这个方法...
<块引用>传播[s]最后一个有效观察结果到下一个有效观察
相反,还有一个 bfill
方法.
此方法不会就地修改 DataFrame - 您需要将返回的 DataFrame 重新绑定到变量或指定 inplace=True
:
df.fillna(method='ffill', inplace=True)
Suppose I have a DataFrame with some NaN
s:
>>> 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
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
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?
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
This method...
propagate[s] last valid observation forward to next valid
To go the opposite way, there's also a bfill
method.
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屋!