我有一个 Pandas df,它有 2 列Day (Date in datetime format) 和Number of Troubleshootings。
我想创建两个新列,第一个是上一个故障事件的天数(我已经完成了),第二个是下一个故障事件的天数(我很难做到)。
Day Number of breakdowns Days from Previous Breakdown Event
2017-01-09 0.0 0
2017-01-12 0.0 0
2017-01-13 0.0 0
2017-01-14 0.0 0
2017-01-16 1.0 0
2017-01-17 0.0 1
2017-01-18 0.0 2
2017-01-19 1.0 0
2017-01-20 0.0 1
2017-01-21 0.0 2
2017-01-23 1.0 0
上次故障事件的天数计算自故障发生以来经过的天数。
代码:
s = df.groupby(df['Number of breakdowns'].ne(0).cumsum())['Day'].transform('first')
df['Days from Previous Breakdown Event'] = (df['Day'] - s).dt.days
zeros_index = df['Number of breakdowns'].ne(0).idxmax()
df.loc[:zeros_index,'Days from Previous Breakdown Event'] = 0
慕少森
相关分类