gpt4 book ai didi

python - 零填充数据帧正向填充

转载 作者:太空狗 更新时间:2023-10-30 02:18:03 24 4
gpt4 key购买 nike

我正在尝试用零填充数据框,但是我不想触及前导 NaN:

rng = pd.date_range('2016-06-01', periods=9, freq='D')
df = pd.DataFrame({'data': pd.Series([np.nan]*3 + [20, 30, 40] + [np.nan]*3, rng)})

2016-06-01 NaN
2016-06-02 NaN
2016-06-03 NaN
2016-06-04 20.0
2016-06-05 30.0
2016-06-06 40.0
2016-06-07 NaN
2016-06-08 NaN
2016-06-09 NaN

填充/替换后我想要的df是这样的:

pd.DataFrame({'data': pd.Series([np.nan]*3 + [20, 30, 40] + [0.]*3, rng)})

2016-06-01 NaN
2016-06-02 NaN
2016-06-03 NaN
2016-06-04 20.0
2016-06-05 30.0
2016-06-06 40.0
2016-06-07 0.0
2016-06-08 0.0
2016-06-09 0.0

由于 fillna() 只允许值或方法并且 fillna(0) 替换所有 NaN,包括前导,我希望替换可以跳到这里,但是

df.replace([np.nan], 0, method='ffill')

也替换所有 NaN。

如何仅在第一个非 NaN 值之后对多个数据列进行零填充?

最佳答案

你可以使用 last_valid_index() 来做到这一点功能:

In [80]: df
Out[80]:
data data1 data2
2016-06-01 NaN NaN NaN
2016-06-02 NaN NaN 10.0
2016-06-03 NaN 20.0 20.0
2016-06-04 20.0 30.0 20.0
2016-06-05 NaN 40.0 NaN
2016-06-06 40.0 30.0 40.0
2016-06-07 NaN NaN NaN
2016-06-08 NaN NaN NaN
2016-06-09 NaN NaN NaN

In [81]: %paste
first_valid_idx = df.apply(lambda x: x.first_valid_index()).to_frame()
df = df.fillna(0)
for ix, r in first_valid_idx.iterrows():
df.loc[df.index < r[0], ix] = np.nan
## -- End pasted text --

In [82]: df
Out[82]:
data data1 data2
2016-06-01 NaN NaN NaN
2016-06-02 NaN NaN 10.0
2016-06-03 NaN 20.0 20.0
2016-06-04 20.0 30.0 20.0
2016-06-05 0.0 40.0 0.0
2016-06-06 40.0 30.0 40.0
2016-06-07 0.0 0.0 0.0
2016-06-08 0.0 0.0 0.0
2016-06-09 0.0 0.0 0.0

In [83]: first_valid_idx
Out[83]:
0
data 2016-06-04
data1 2016-06-03
data2 2016-06-02

旧答案:

In [38]: df.loc[df.index > df.data.last_valid_index(), 'data'] = 0

In [39]: df
Out[39]:
data
2016-06-01 NaN
2016-06-02 NaN
2016-06-03 NaN
2016-06-04 20.0
2016-06-05 30.0
2016-06-06 40.0
2016-06-07 0.0
2016-06-08 0.0
2016-06-09 0.0

关于python - 零填充数据帧正向填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37810688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com