1

I have a dataset here with 4 columns and 12 rows:

A       SIGMA.DAT                  WATER     nan
A       CUR.X.HEPTANE.dat          OIL       2.1373003
A       CUR.Y.HEPTANE.dat          OIL       2.1797340
B       SIGMA.DAT                  WATER     2.98415
B       CUR.X.HEPTANE.dat          OIL       4.2373003
B       CUR.Y.HEPTANE.dat          OIL       0.1497340
C       SIGMA.DAT                  WATER     7.98415
C       CUR.X.HEPTANE.dat          OIL       0.1373003
C       CUR.Y.HEPTANE.dat          OIL       3.1567340
D       SIGMA.DAT                  WATER     
D       CUR.X.HEPTANE.dat          OIL       0.1343003
D       CUR.Y.HEPTANE.dat          OIL       

I want to transform in into this dataframe:

          SIGMA.DAT            CUR.X.HEPTANE.dat      CUR.Y.HEPTANE.dat
A         nan                  2.1373003              2.179734
B         2.98415              4.2373003              0.1497340
C         7.98415              0.1373003              3.1567340
D         nan                  0.1343003              nan

Thanks for helping me out :)

Dan Ni Lin
  • 11
  • 1
  • 2
    Does this answer your question? [How to pivot a dataframe?](https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe) – BigBen May 12 '21 at 19:00
  • 1
    Also, https://stackoverflow.com/questions/28337117/how-to-pivot-a-dataframe-in-pandas – BigBen May 12 '21 at 19:01

1 Answers1

0

use pivot_table:

NOTE: I'm using the 1st row as column names here. Please, make the required changes.

result = df.pivot_table(index=['A'], columns=['SIGMA.DAT'], values='nan')

OUTPUT:

SIGMA.DAT  CUR.X.HEPTANE.dat  CUR.Y.HEPTANE.dat  SIGMA.DAT
A                                                         
A                     2.1373           2.179734        NaN
B                     4.2373           0.149734    2.98415
C                     0.1373           3.156734    7.98415
D                     0.1343                NaN        NaN
Nk03
  • 14,699
  • 2
  • 8
  • 22