熊猫事件搜索

问题描述 投票:0回答:1

我有一个数据框,我想创建一个带有事件标签的列。如果条件为真,则事件将获得一个数字。但是,如果连续值是事件,我想给相同的事件标签。你有什么主意吗?我尝试使用.apply和.rolling,但没有成功。

DataFrame:

df = pd.DataFrame({'Signal_1' : [0,0,0,1,1,0,0,1,1,1,1,0,0,0,1,1,1,1,1]})

    Signal_1  ExpectedColumn
0          0             NaN 
1          0             NaN
2          0             NaN
3          1               1
4          1               1
5          0             NaN
6          0             NaN
7          1               2
8          1               2
9          1               2
10         1               2
11         0             NaN
12         0             NaN
13         0             NaN
14         1               3
15         1               3
16         1               3
17         1               3
18         1               3
python pandas events label flags
1个回答
0
投票

这里是一种方法。首先创建计数标志,然后执行累加。然后使用NaN值对其进行更正。

import pandas as pd
import numpy as np

df = pd.DataFrame({'Signal_1' : [0,0,0,1,1,0,0,1,1,1,1,0,0,0,1,1,1,1,1]})

# Only count up when the previous sample = 0, and the current sample = 1
df["shift"] = df["Signal_1"].shift(1)
df["countup"] = np.where((df["Signal_1"] == 1) & (df["shift"] == 0),1,0)

# Cumsum the countup flag and set to NaN when sample = 0
df["result"] = df["countup"].cumsum()
df["result"] = np.where(df["Signal_1"] == 0, np.NaN, df["result"] )

© www.soinside.com 2019 - 2024. All rights reserved.