如何创建新的列,可以使用Pandas合并相邻但已分解的“on”值组合成组

iovurdzv  于 2023-03-21  发布在  其他
关注(0)|答案(1)|浏览(79)

我有一个 Dataframe ,其中一列是1(对应于“ON”信号)和0(对应于“OFF”信号)。
我的数据中有一些噪声,比如第一个“ON”信号中间有一些0,这使它看起来像是被分解了。其他“ON”信号也是如此。这使得很难计算我的数据中总共有多少个“ON”信号。看起来好像有比有多!
我有没有办法将这些信号分组,填补空白?理想情况下,我希望创建一个新列,指示截至该时间的“ON”信号的当前数量。x1c 0d1x
我已经尝试了滚动平均值和阈值类型的方法...任何帮助将不胜感激。
这给了我它从“开”变成“关”的位置,让我查找它发生时的时间索引:

df.loc[:,'Change'] = np.abs(df['ONOFF_Signal'].diff())
On_off_timestamps = df.query("Change == 1")['Time'].values

##Sample_data for two "ON" groupings. 
df['Change'] = [0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,1,1,0,0,0,0,0,0]

#Output wanted: 
[0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0]

##So that I can generate a count column "mask" up to each row of how many "ON" values have occurred: 
[0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
fnvucqvd

fnvucqvd1#

对,经过很多的折腾,这似乎起作用了....我很想听听有没有更好的方法来做这件事。我的方法很乱!!

#Set the gap length to detect
    gap_length = 40
        
    #Create a boolean mask to identify where signal changes occur
    mask = df['change'].ne(df['change'].shift()) | (df['change'].rolling(gap_length+1).sum() == 0)
        
    #Generate a grouping identifier for each contiguous sequence of "ON" signals
    group_id = mask.cumsum()
        
    #Use groupby and cumcount to generate a count of "ON" signals up to each row
    df['mask'] = df.groupby(group_id).cumcount() + 1
        
    #Replace the 0's with NaN values
    df['mask'][df['mask'] > 1] = 0
        
    #Apply rolling window - signal inverted, so *1+1 to flip it and center it 
    df['mask'] = (df['mask'].rolling(window=3, center=True).min()*-1)+1

    # pick out where the filled in mask changes
    df['on_number'] = d['mask'].diff()

    # Keep only the first "ON" signal
    df['on_number'][df['on_number'] < 0] = 0

    # Sum on signals to get a rolling count of true "ONs"
    df['on_number'] = df['on_number'].cumsum()

相关问题