Skip to content
Advertisement

How to merge rows in a Dataframe based on a previous row?

I have a sequentially ordered dataframe that represent two events measured over time – the measurements are the start and end times of the event. They should be ordered in an ABABAB sequence, but in some cases I may have consecutive events of the same type (i.e. ABABAABABB). I am looking for a way to check the event label (A or B) in each row with the previous event label, and if they are the same to merge the rows in such a way that I maintain the start time of the first event and the end time of the second event. Consider the following:

JavaScript

What I currently have…

JavaScript

What I need…

Note: The two A events at index position 2-3 have been merged into one, as have the two B events originally at positions 4-5.

JavaScript

I had initially thought to use groupby but I don’t think this right as this will group over the entire dataframe. Similarly I have tried using iteritems but have not had any success. Apologies for the lack of code but I’m at a loss as to how to approach the problem.

Advertisement

Answer

You can use GroupBy.agg with first and last.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement