I have a pandas dataframe composed by 3 columns.
index start end value
0 0 37647 0
1 37648 37846 1
2 37847 42874 0
3 42875 43049 1
4 43050 51352 0
5 51353 51665 -1
6 51666 54500 0
7 54501 54501 -1
8 54502 55259 0
I would like to implement a check on the difference between start and end of each row. In particular what I would like to do is:
if end row x - start row x == 0 incorporate this row in the previous row
For example the 8th row
7 54501 54501 -1
has end - start = 0. I would like to modify the dataframe like this
index start end value
0 0 37647 0
1 37648 37846 1
2 37847 42874 0
3 42875 43049 1
4 43050 51352 0
5 51353 51665 -1
6 51666 54501 0
7 54502 55259 0
and then since the 7th and the 8th row now have the same "value" it should become
0 0 37647 0
1 37648 37846 1
2 37847 42874 0
3 42875 43049 1
4 43050 51352 0
5 51353 51665 -1
6 51666 55259 0
EDITED
Please note that a particular case would be
index start end value
0 0 37647 0
1 37648 37846 1
2 37847 42874 0
3 42875 43049 1
4 43050 51352 0
5 51353 51665 -1
6 51666 54500 0
7 54501 54501 -1
8 54502 54502 0
9 54503 55259 1
In this case there are 2 consecutive rows (8th and 9th) for which the difference between end and start values is 0. In this case the answer proposed gives an error since the index 7th was deleted previously. I solved this case using a while loop instead of a for loop, but I guess it is not the best thing to do.
For this case we should have
index start end value
0 0 37647 0
1 37648 37846 1
2 37847 42874 0
3 42875 43049 1
4 43050 51352 0
5 51353 51665 -1
6 51666 54502 0
7 54503 55259 1