如何使用colmun名称旋转pandas.dataframe

Chiou Chiou Aymen:

我有以下DataFrame,以Timestamp作为索引:

Timestamp 40_x 40_y 40_z 60_x 60_y 60_z 80_x 80_y 80_z
    10:00    a    b    c    d    e    f    g    h    i
    10:10   a2   b2   c2   d2   e2   f2   g2   h2   i2

我想将其旋转到以下DataFrame中:

                  x   y   z
Timestamp range               
10:00        40   a   b   c
10:00        60   d   e   f
10:00        80   g   h   i
10:10        40  a2  b2  c2
10:10        60  d2  e2  f2
10:10        80  g2  h2  i2

我有一个范围列表:[40, 60, 80],它可以包含更多值。

Datanovice:

怎么样MultiIndex,然后stack

df.columns = df.columns.str.split('_',expand=True)

print(df)

           40          60          80        
            x   y   z   x   y   z   x   y   z
Timestamp                                    
10:00       a   b   c   d   e   f   g   h   i
10:10      a2  b2  c2  d2  e2  f2  g2  h2  i2

df1 = df.stack(0)

print(df1)

               x   y   z
Timestamp               
10:00     40   a   b   c
          60   d   e   f
          80   g   h   i
10:10     40  a2  b2  c2
          60  d2  e2  f2
          80  g2  h2  i2

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章