且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何按 NAN 值拆分 pandas 时间序列

更新时间:2023-11-20 20:06:58

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

You can use numpy.split and then filter the resulting list. Here is one example assuming that the column with the values is labeled "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 值分隔的所有事件.

You will have a list with all the events separated by the NaN values.