条件满足时填充nan值
Fill nan value when condition meet
我有一个如下所示的数据框:
df = pd.DataFrame({'A': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, np.nan, np.nan, np.nan, 6, 7, 8, 9, 10,np.nan, np.nan, 10, 11]})
我只想在前一个非 nan 值等于流动的非 nan 值时填充 nan 值。
预期输出:
df = pd.DataFrame({'A': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, np.nan, np.nan, np.nan, 6, 7, 8, 9, 10, np.nan, np.nan,10, 11],'fill_nan': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 8, 9, 10, 10, 10,10, 11]})
尝试将 bfill
与 ffill
进行比较:
s = df.ffill()
out = s.where(s.eq(df.bfill()))
输出:
A
0 1.0
1 NaN
2 NaN
3 NaN
4 3.0
5 3.0
6 3.0
7 3.0
8 NaN
9 NaN
10 NaN
11 5.0
12 5.0
13 5.0
14 6.0
15 6.0
16 6.0
17 6.0
18 6.0
19 6.0
20 6.0
21 7.0
22 8.0
23 9.0
24 10.0
25 10.0
26 10.0
27 10.0
28 11.0
我有一个如下所示的数据框:
df = pd.DataFrame({'A': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, np.nan, np.nan, np.nan, 6, 7, 8, 9, 10,np.nan, np.nan, 10, 11]})
我只想在前一个非 nan 值等于流动的非 nan 值时填充 nan 值。
预期输出:
df = pd.DataFrame({'A': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, np.nan, np.nan, np.nan, 6, 7, 8, 9, 10, np.nan, np.nan,10, 11],'fill_nan': [1, np.nan, np.nan, np.nan, 3, 3, 3, 3, np.nan, np.nan, np.nan, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 8, 9, 10, 10, 10,10, 11]})
尝试将 bfill
与 ffill
进行比较:
s = df.ffill()
out = s.where(s.eq(df.bfill()))
输出:
A
0 1.0
1 NaN
2 NaN
3 NaN
4 3.0
5 3.0
6 3.0
7 3.0
8 NaN
9 NaN
10 NaN
11 5.0
12 5.0
13 5.0
14 6.0
15 6.0
16 6.0
17 6.0
18 6.0
19 6.0
20 6.0
21 7.0
22 8.0
23 9.0
24 10.0
25 10.0
26 10.0
27 10.0
28 11.0