使用pandas旋转多个列

时间:2018-02-27 19:08:57

标签: python pandas

我这里有一个数据框

-f[no-]computed-goto

我想转动数据,以便我有四个新列: Row dist1 dist2 dist3 variable value Smallest Group 0 0 40 101 dist2 40 Smallest SmallestGroup 1 0 40 101 dist3 101 SecondSmallest SecondSmallestGroup 2 1 30 100 dist2 30 Smallest SmallestGroup 3 1 30 100 dist3 100 SecondSmallest SecondSmallestGroup 4 2 30 20 98 dist2 20 Smallest SmallestGroup 5 2 30 20 98 dist1 30 SecondSmallest SecondSmallestGroup 6 3 20 15 72 dist2 15 Smallest SmallestGroup 7 3 20 15 72 dist1 20 SecondSmallest SecondSmallestGroup 8 4 15 16 11 dist3 11 Smallest SmallestGroup 9 4 15 16 11 dist1 15 SecondSmallest SecondSmallestGroup Smallest(其中包含SecondSmallest列中的值)和valueSmallestGroup(其中包含SecondSmallestGroup列中的值。)

我想要的输出如下:

variable

我确信我可以通过一个支点实现这个目标,但我不确定如何构建命令。

2 个答案:

答案 0 :(得分:2)

因为,我不认为原始数据指明了' Group'和'变量'必须是相关联的,并且是最小的'和'价值'在一起,然后我们必须做这两个步骤并连接。

让我们试试这个:

(pd.concat([df.set_index(['Row','dist1','dist2','dist3','Group'])['variable'].unstack(),
            df.set_index(['Row','dist1','dist2','dist3','Smallest'])['value'].unstack()],
           axis=1)
   .reset_index())

输出:

   Row  dist1  dist2  dist3 SecondSmallestGroup SmallestGroup  SecondSmallest  Smallest
0    0    NaN     40    101               dist3         dist2             101        40
1    1    NaN     30    100               dist3         dist2             100        30
2    2   30.0     20     98               dist1         dist2              30        20
3    3   20.0     15     72               dist1         dist2              20        15
4    4   15.0     16     11               dist1         dist3              15        11

答案 1 :(得分:0)

df[['Row','dist1','dist2','dist3']].groupby('Row').first().join(
    df.pivot_table(values = 'value',index= 'Row',columns = ['Smallest'],aggfunc='first')).join(
    df.pivot_table(values = 'variable',index= 'Row',columns = ['Group'],aggfunc='first'))   

输出:

     dist1  dist2  dist3  SecondSmallest  Smallest SecondSmallestGroup  \
Row                                                                      
0      NaN     40    101             101        40               dist3   
1      NaN     30    100             100        30               dist3   
2     30.0     20     98              30        20               dist1   
3     20.0     15     72              20        15               dist1   
4     15.0     16     11              15        11               dist1   
SmallestGroup  
Row                
0           dist2  
1           dist2  
2           dist2  
3           dist2  
4           dist3