Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group rows sequentially in python

I have a data set as such and I need to group them sequentially, and join the dates.

number, date 

123456,2021-01-16
123456,2021-01-18
98765,2021-01-19 
98765,2021-02-01
123456,2021-02-02
123456,2021-02-03
123456,2021-02-09
123456,2021-02-11
7645323,2021-02-13
7645323,2021-02-16
7645323,2021-02-17

I want the output to be like,

number,date 

123456, 2021-01-16 to 2021-01-18
98765, 2021-01-19 to 2021-02-01
123456, 2021-02-02 to 2021-02-11
7645323, 2021-02-13 to 2021-02-17

This is my code, referred from this answer is very close, but can’t work on dates, and I’m not able to figure out

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df.groupby(df.number!=df.number.shift())['date'].transform(lambda x: ','.join(x)).reset_index(drop=True)

I do get an output, but somewhat like this, not having number at all.

enter image description here

NOTE:

Link to test data and output data

>Solution :

You can do with shift and cumsum create the groupby key

key = df.number.ne(df.number.shift()).cumsum()
out = df.groupby(key).agg({'number':'first',
                           'date' : lambda x : x.iloc[0] + ' to ' +  x.iloc[-1]})
Out[822]: 
         number                       date
number                                    
1        123456   2021-01-16 to 2021-01-18
2         98765  2021-01-19  to 2021-02-01
3        123456   2021-02-02 to 2021-02-11
4       7645323   2021-02-13 to 2021-02-17
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading