1

I have a pandas DF:-

    start end   ent
        0   2   O
        3   6   O
        7   10  O
        11  17  ABC
        18  20  O
        21  24  O
        25  29  O
        30  32  O
        33  37  XYZ
        38  41  XYZ
        42  46  XYZ
        47  50  O
        51  55  O
        56  59  O
        60  64  O
        65  67  O
        68  72  O
        72  75  O
        76  79  O
        79  82  O
        82  85  O

The goal is to compare 'end' value of a row with 'start' value of row+1 and if they are equal the above would result into:-

    start end   ent
        0   2   O
        3   6   O
        7   10  O
        11  17  ABC
        18  20  O
        21  24  O
        25  29  O
        30  32  O
        33  37  XYZ
        38  41  XYZ
        42  46  XYZ
        47  50  O
        51  55  O
        56  59  O
        60  64  O
        65  67  O
        68  75  O
        76  85  O

Last 5 rows of original df have changed.

Would really appreciate any inputs, Thank you.

2
  • 1
    Not a downvoter but probably you should detail your effort towards solving your problem and where you got stuck. Commented Sep 14, 2020 at 15:33
  • Will add my code also from next time, Thanks Quang :) Commented Sep 14, 2020 at 15:43

1 Answer 1

2

To get the previous values, we can use shift(). And the common technique to identify these blocks is cumsum on the negated condition, then groupby:

mask = df['start'] != df['end'].shift()

df.groupby(mask.cumsum()).agg({'start':'first', 'end':'last', 'ent':'first'})

Output:

    start  end  ent
1       0    2    O
2       3    6    O
3       7   10    O
4      11   17  ABC
5      18   20    O
6      21   24    O
7      25   29    O
8      30   32    O
9      33   37  XYZ
10     38   41  XYZ
11     42   46  XYZ
12     47   50    O
13     51   55    O
14     56   59    O
15     60   64    O
16     65   67    O
17     68   75    O
18     76   85    O
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.