简体   繁体   English

如何在pandas DataFrame中将二级索引更改为二级列?

[英]How to change 2nd level index into 2nd level column in pandas DataFrame?

Say I have the following pandas DataFrame: 说我有以下pandas DataFrame:

                          variable1     variable2
 proton_energy quantile                           
 5             0.16      36653.98080   82638.76640
               0.50      42634.57000   91758.91000
               0.84      50304.00280  102421.34800
 10            0.16      18617.96840   40715.77400
               0.50      22894.68500   48133.44500
               0.84      30622.25280   59162.39640
 ...

Maybe I am missing something really trivial, but I could not come up with an easy way how to end with this schema (values omitted): 也许我错过了一些非常简单的东西,但我无法想出一个简单的方法来结束这个模式(值省略):

                                    variable1              variable2
  proton_energy     quantile   0.16   0.50   0.84    0.16     0.50   0.84
  5                 .............................  ......................
  10                .............................  ......................
  ...

In other words, I want to change the 2nd-level index into 2nd-level column. 换句话说,我想将第二级索引更改为第二级列。 Any ideas but for a set of for loops and manually constructing a new DataFrame? 任何想法,但一组for循环和手动构建一个新的DataFrame?

Thank you 谢谢

Ok, I figured it out: 好的,我弄清楚了:

df.unstack(level=-1)

does exactly the job. 完成这项工作。

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

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