简体   繁体   English

如果为 NaN,则滚动数据框 pandas 并更新值

[英]Rolling over dataframe pandas and update values if NaN

I want to use rolling function on my dataframe.我想在我的数据框上使用滚动功能。 Here my series :这是我的系列:

index指数 date日期 id ID value_dex value_dex
90256 90256 2021-05-10 01:45:20 2021-05-10 01:45:20 101904285 101904285 7.6 7.6
90257 90257 2021-05-10 01:45:20 2021-05-10 01:45:20 101904285 101904285 7.6 7.6
90258 90258 2021-05-10 02:00:00 2021-05-10 02:00:00 101904285 101904285 NaN
90260 90260 2021-05-10 02:00:44 2021-05-10 02:00:44 101904285 101904285 6.9 6.9
90261 90261 2021-05-10 02:00:44 2021-05-10 02:00:44 101904285 101904285 NaN

What I want :我想要的是 :

index指数 date日期 id ID value_dex value_dex
90256 90256 2021-05-10 01:45:20 2021-05-10 01:45:20 101904285 101904285 7.6 7.6
90257 90257 2021-05-10 01:45:20 2021-05-10 01:45:20 101904285 101904285 7.6 7.6
90258 90258 2021-05-10 02:00:00 2021-05-10 02:00:00 101904285 101904285 7.6 7.6
90260 90260 2021-05-10 02:00:44 2021-05-10 02:00:44 101904285 101904285 6.9 6.9
90261 90261 2021-05-10 02:00:44 2021-05-10 02:00:44 101904285 101904285 6.9 6.9

I would like to drag the values in the column over 30 minutes if NaN.如果 NaN,我想将列中的值拖动超过 30 分钟。 However, I also want the dragging to stop when a new value is encountered on the 30 minute range (in this case, we drag the new value over 30 minutes).但是,我还希望在 30 分钟范围内遇到新值时停止拖动(在本例中,我们将新值拖动超过 30 分钟)。

I tried that :我试过了:


ffill = lambda x: pd.DataFrame(x.rolling('30T', on='Date')[y].max(),
                                       index=x.index)
        
        data[y] = data.groupby('ID', as_index=False).apply(ffill)

But it doesn't work when there is a new value during the rolling.但是在滚动过程中有新值时它不起作用。 Do you have any solutions in order to resolve my problem ?您有任何解决方案来解决我的问题吗? Maybe an existing function ?也许是现有的功能? Thanks a lot !非常感谢 !

我相信您可以尝试将其用作 NaN 值的填充物:

data.fillna(method='ffill')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM