fileter the data, use index - 1
to get the previous row. then append the origin dataframe.
cond = (df['a'] == "POWER_CHANGE") & (df['Indicator'] > 1)
# calculate the time is 1 second before POWER_CHANGE
obj_time = (pd.to_datetime(df.loc[cond, 'time']) -
pd.to_timedelta('00:00:01')).dt.strftime('%T')
# get the previous record before 'POWER_CHANGE'
idx = df.loc[cond].index - 1
dfn = df.loc[idx].copy()
# reassign the time, use `.values` to ignore the index
dfn['time'] = obj_time.values
df.append(dfn).sort_index()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…