1 回答
TA贡献1873条经验 获得超9个赞
你可以用
# get where the starts are not empty
arr_start = df.filter(like='Start').notna().to_numpy()
# get the end columns
df_end = df.filter(like='End')
# fillna the right missing value
df = df.fillna(df_end.bfill(axis=1)[arr_start&df_end.isna()])
# remove the values of the last step end if no value in the start of this step
df.loc[~arr_start[:,-1], df_end.columns[-1]] = np.nan
print (df)
Project Step 1 Start Step 1 End Step 2 Start Step 2 End Step 3 Start \
0 A 01.01.2020 02.01.2020 02.01.2020 03.01.2020 03.01.2020
1 B 01.02.2020 02.02.2020 02.02.2020 03.02.2020 NaN
2 C 01.03.2020 02.03.2020 02.03.2020 03.03.2020 NaN
3 D 01.04.2020 02.04.2020 NaN NaN NaN
4 E 01.05.2020 02.05.2020 02.05.2020 03.05.2020 NaN
Step 3 End Step 4 Start Step 4 End Step 5 Start Step 5 End Finish
0 04.01.2020 04.01.2020 05.01.2020 05.01.2020 06.01.2020 06.01.2020
1 NaN NaN NaN 03.02.2020 04.02.2020 04.02.2020
2 NaN NaN NaN NaN NaN 03.03.2020
3 NaN NaN NaN NaN NaN 02.04.2020
4 NaN 03.05.2020 04.05.2020 NaN NaN 04.05.2020
多一个解释,arr_start&df_end.isna()允许发现哪个步骤有开始而不是结束,使用bfill将有助于获得最后一步结束到需要填充的结束步骤的值
添加回答
举报