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

Merge rows pandas based on key

Suposse we have this df:

id dates
0 12 2012-12-06
1 12 2012-12-07
2 13 2012-01-02
3 13 2012-01-03
4 14 2012-12-06

How could I merge the rows based on the unique id values and store all other variables with ,together (let’s say date or even more columns), for example:

id all dates
0 12 2012-12-06 , 2012-12-07
1 13 2012-01-02 , 2012-01-03
2 14 2012-12-06

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

>Solution :

Based on this you can adjust for extra columns as you’d like.

df_new['date2']=df_new.groupby('name').shift(-1)
df_new['date2']=';'+df_new['date2']
df_new['date2']=df_new['date2'].fillna('')
df_new['date3']=df_new['date']+df_new['date2']
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