gpt4 book ai didi

python - 如何按 NAN 值拆分 Pandas 时间序列

转载 作者:太空狗 更新时间:2023-10-30 00:32:51 25 4
gpt4 key购买 nike

我有一个 pandas TimeSeries,它看起来像这样:

2007-02-06 15:00:00    0.780
2007-02-06 16:00:00 0.125
2007-02-06 17:00:00 0.875
2007-02-06 18:00:00 NaN
2007-02-06 19:00:00 0.565
2007-02-06 20:00:00 0.875
2007-02-06 21:00:00 0.910
2007-02-06 22:00:00 0.780
2007-02-06 23:00:00 NaN
2007-02-07 00:00:00 NaN
2007-02-07 01:00:00 0.780
2007-02-07 02:00:00 0.580
2007-02-07 03:00:00 0.880
2007-02-07 04:00:00 0.791
2007-02-07 05:00:00 NaN

每当连续出现一个或多个 NaN 值时,我想拆分 pandas TimeSeries。我的目标是分离事件。

Event1:
2007-02-06 15:00:00 0.780
2007-02-06 16:00:00 0.125
2007-02-06 17:00:00 0.875

Event2:
2007-02-06 19:00:00 0.565
2007-02-06 20:00:00 0.875
2007-02-06 21:00:00 0.910
2007-02-06 22:00:00 0.780

我可以循环遍历每一行,但还有一种聪明的方法吗???

最佳答案

您可以使用 numpy.split 然后过滤结果列表。下面是一个示例,假设具有值的列标记为 "value":

events = np.split(df, np.where(np.isnan(df.value))[0])
# removing NaN entries
events = [ev[~np.isnan(ev.value)] for ev in events if not isinstance(ev, np.ndarray)]
# removing empty DataFrames
events = [ev for ev in events if not ev.empty]

您将得到一个列表,其中包含由 NaN 值分隔的所有事件。

关于python - 如何按 NAN 值拆分 Pandas 时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21402384/

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