2

I currently have the following dataframe:

Current df:

            type       part number    part description  waiting period  hours  
0   service item     SOME-X1R-1807                SOME           1 day     24  
1     CONSUMABLE   RANDOM-462-1171         DESCRIPTION          6 days    144  
2          wheel      PART-7W-2326           ABOUT THE          7 days    168  
3           tyre    NUMBER-1R-0762      PARTS FROM THE          8 days    192  
4          other    NUMBER-XL-0747     PREVIOUS COLUMN          9 days    216

I want to pivot the table, and get the following outcome:

Expected df:

                        SOME-X1R-1807    RANDOM-462-1171    PART-7W-2326     NUMBER-1R-076     NUMBER-XL-0747 
part description                 SOME        DESCRIPTION       ABOUT THE    PARTS FROM THE    PREVIOUS COLUMN
            type         service item         CONSUMABLE           wheel              tyre              other
  waiting period                1 day             6 days          7 days            8 days             9 days 
           hours                   24                144             168               192                216

Question:

How do I pivot a dataframe to get this output?

Things I have tried:

smci
  • 32,567
  • 20
  • 113
  • 146
3kstc
  • 1,871
  • 3
  • 29
  • 53
  • 1
    Links don't, in themselves, show us your existing code and where it might be failing. Please provide a [mcve]. – jpp Mar 09 '18 at 02:02
  • @jpp I was trying `pivot` which is the wrong approach, it should be `set_index` – 3kstc Mar 09 '18 at 02:14
  • *"transform so columns become rows"* = **transpose** – smci May 24 '18 at 11:48
  • This should be mentioned as a one-liner in both the pandas doc and SO canonicals you mention. – smci May 24 '18 at 11:49
  • In contrast **pivot** keeps the vertical column layout, it just expands/collapses levels of specified variables into new columns. – smci May 24 '18 at 11:52

1 Answers1

4

I do not think this is pivot

df.set_index('part number').T
Out[214]: 
part number         SOME-X1R-1807  RANDOM-462-1171     PART-7W-2326  \
            type     service item       CONSUMABLE            wheel   
part description             SOME       ESCRIPTION        ABOUT THE   
waiting period              1 day           6 days           7 days   
hours                          24              144              168   
part number        NUMBER-1R-0762   NUMBER-XL-0747  
            type             tyre            other  
part description   PARTS FROM THE  PREVIOUS COLUMN  
waiting period             8 days           9 days  
hours                         192              216  
BENY
  • 317,841
  • 20
  • 164
  • 234