1 回答
TA贡献2019条经验 获得超9个赞
iloc[::-1]用transform和交换顺序last,然后也交换s - df['Day']:
s = df.groupby(df['Number of breakdowns'].iloc[::-1].ne(0).cumsum())['Day'].transform('last')
df['Days from Next Breakdown Event'] = (s - df['Day']).dt.days
print (df)
Day Number of breakdowns Days from Previous Breakdown Event \
0 2017-01-09 0.0 0
1 2017-01-12 0.0 0
2 2017-01-13 0.0 0
3 2017-01-14 0.0 0
4 2017-01-16 1.0 0
5 2017-01-17 0.0 1
6 2017-01-18 0.0 2
7 2017-01-19 1.0 0
8 2017-01-20 0.0 1
9 2017-01-21 0.0 2
10 2017-01-23 1.0 0
Days from Next Breakdown Event
0 7
1 4
2 3
3 2
4 0
5 2
6 1
7 0
8 3
9 2
10 0
详情:
print (s)
0 2017-01-16
1 2017-01-16
2 2017-01-16
3 2017-01-16
4 2017-01-16
5 2017-01-19
6 2017-01-19
7 2017-01-19
8 2017-01-23
9 2017-01-23
10 2017-01-23
Name: Day, dtype: datetime64[ns]
添加回答
举报