Python: Find the first occurance of 1 in a column within a group and repeat the values for subsequent rows in that group
You can groupby
"ID" and find the cumulative max for each group using cummax
and assign them to new columns:
df[['of_flag_up','os_flag_up']] = df.groupby('ID')[['of_flag', 'os_flag']].cummax()
Output:
ID of_flag os_flag of_flag_up os_flag_up
0 1 0 1 0 1
1 1 1 0 1 1
2 2 1 0 1 0
3 2 0 1 1 1
4 2 0 0 1 1