I want something like this:
Id A B Flag COL
1 5 4 0 0
1 5 8 1 1
1 6 4 0 1
1 4 7 1 2
2 7 6 0 0
2 8 9 1 1
2 3 8 1 2
I have dataframe that has to be partitioned based on id and I have flag based on condition(A<B,Then 1) and I need to get rows in column based on previous row. Logic is if flag is 1, then COL will be previous row value+1 else if flag is 0, the COL will be value of previous row of column itself. P.S- We don't have COL column in df, I am creating it based on above logic. My output should be like above mentioned table.
ID=1may come one after the other ... on paper, your logic works, but not with dataframe. Please, show your real data, not some example you think is simpler, because it is not. - Steven0,0,1,2or1,2,2,2or any other combinaison.We need both this new column and the previous one. - Steven