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

combine rows with the same IDs into the same row

I have a dataset with 600 rows. the data has one main ID= Version and second ID= Task. Data looks like this:

 Version  Task  Concept  Att 1 -  Att 2 -
       1     1        1        3        2
       1     1        2        1        1
       1     2        1        2        3
       1     2        2        1        2
       1     3        1        2        3
       1     3        2        3        1
       2     1        1        2        1
       2     1        2        3        2
       2     2        1        2        2
       2     2        2        1        3
       2     3        1        3        1
       2     3        2        1        3

I would like to change the format, so to have "Task" which belongs to the same "Version" in the same row like this:

 Version  Task  Concept  Att 1 -  Att 2 -  Version  Task  Concept  Att 1 -  Att 2 -
       1     1        1        3        2        1     1        2        1        1
       1     2        1        2        3        1     2        2        1        2
       1     3        1        2        3        1     3        2        3        1
       2     1        1        2        1        2     1        2        3        2
       2     2        1        2        2        2     2        2        1        3
       2     3        1        3        1        2     3        2        1        3

I have tried different things like groupby, pivot but I cannot find the right solution

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 :

I think a pivot is the clean way to reshape (df.pivot(index=['Version', 'Task'], columns='Concept'), optionally with flattening the columns MultiIndex).

That said if you really want to duplicate the columns, you could combine a groupby and concat:

out = (pd.concat([g.set_index(['Version', 'Task'], drop=False)
                 for k, g in df.groupby('Concept')], axis=1)
         .reset_index(drop=True)
      )

Output:

   Version  Task  Concept  Att 1 -  Att 2 -  Version  Task  Concept  Att 1 -  Att 2 -
0        1     1        1        3        2        1     1        2        1        1
1        1     2        1        2        3        1     2        2        1        2
2        1     3        1        2        3        1     3        2        3        1
3        2     1        1        2        1        2     1        2        3        2
4        2     2        1        2        2        2     2        2        1        3
5        2     3        1        3        1        2     3        2        1        3
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