Skip to content
Advertisement

How do I group into different dates based on change in another column values in Pandas

I have data that looks like this

JavaScript

enter image description here

What I would like to do is group by ID and CD and get the start and stop change for each change. I tried using groupby and agg function but it will group all A together even though they needs to be separated since there is B in between 2 A.

JavaScript

What I get is :

enter image description here

I was hoping if some one could help me get the result I need. What I am looking for is :

enter image description here

Advertisement

Answer

make grouper for grouping

JavaScript

grouper:

JavaScript

then use groupby with grouper

JavaScript

output:

JavaScript



change column name and use reset_index and so on..for your desired output

JavaScript

result

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