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

Use pandas operations to transpose and reindex

I have the following dataframe:

           Sample ID  'Deinococcus soli' Cha et al. 2014  16SrX (Apple proliferation group)  16SrXII (Stolbur group) 
0   C1day1_barcode01                                  21                                  1                        0
1  C1day21_barcode19                                  22                                  0                        0
2   C3day1_barcode03                                  13                                  0                        0
3  C3day14_barcode15                                  14                                  2                        2
4  T1day21_barcode22                                  19                                  1                        1

This is my desired output:


                              Sample ID  C1day1_barcode01  C1day21_barcode19  C3day1_barcode03  C3day14_barcode15  T1day21_barcode22
0    'Deinococcus soli' Cha et al. 2014                21                 22                13                 14                 19
1     16SrX (Apple proliferation group)                 1                  0                 0                  2                  1
2               16SrXII (Stolbur group)                 0                  0                 0                  2                  1

I have used transpose and I tried inserting a new index & renaming the columns as well as resetting the index but in either cases I get something like this:

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

0                           Sample ID                                     0                                      1                                     2                                          3                                         4
0                           Sample ID  C1day1_barcode01  C1day21_barcode19  C3day1_barcode03  C3day14_barcode15  T1day21_barcode22
1  'Deinococcus soli' Cha et al. 2014                21                 22                13                 14                 19
2   16SrX (Apple proliferation group)                 1                  0                 0                  2                  1
3             16SrXII (Stolbur group)                 0                  0                 0                  2                  1

or

Sample ID                            index  C1day1_barcode01  C1day21_barcode19  C3day1_barcode03  C3day14_barcode15  T1day21_barcode22
0  'Deinococcus soli' Cha et al. 2014                21                 22                13                 14                 19
1   16SrX (Apple proliferation group)                 1                  0                 0                  2                  1
2             16SrXII (Stolbur group)                 0                  0                 0                  2                  1

Though I tried renaming index to Sample ID I’m unable to remove the "Sample ID" title of the index even by setting index.name = None. What do I do?

>Solution :

set_index on the "Sample ID" column to set is aside, transpose to reshape, rename_axis to exchange the axis names, and reset_index to move back the IDs as column:

out = (df.set_index('Sample ID').T
         .rename_axis(index='Sample ID', columns=None)
         .reset_index()
      )

Alternative:

col = 'Sample ID'

out = (df.set_index(col).T
         .rename_axis(index=col, columns=None)
         .reset_index()
      )

Output:

                            Sample ID  C1day1_barcode01  C1day21_barcode19  C3day1_barcode03  C3day14_barcode15  T1day21_barcode22
0  'Deinococcus soli' Cha et al. 2014                21                 22                13                 14                 19
1   16SrX (Apple proliferation group)                 1                  0                 0                  2                  1
2             16SrXII (Stolbur group)                 0                  0                 0                  2                  1
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