简体   繁体   English

如何使用pandas转动数据框,以便变量列成为行?

[英]How to pivot a dataframe with pandas so variable columns become rows?

I currently have the following dataframe: 我目前有以下数据框:

Current df : 目前的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 : 预计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: 我尝试过的事情:

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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM