python-从MultiIndex Pandas数据框中删除一列

我一直在尝试了解熊猫的多索引方法.我正在尝试删除“ std”子列,但徒劳无功.

如何才能做到这一点?

                                    attribute                           attribute2  \
                                        test1            std           test2   
d         count       type                                                  
r1         10          rx      0.559 (0.0)    0.559 (0.0)    0.568 (0.0)   
                     sth1      0.653 (0.004)  0.653 (0.004)  0.679 (0.002)   
                     sth2      0.584 (0.002)  0.584 (0.002)  0.586 (0.003)   
                     sth3      0.651 (0.005)  0.651 (0.005)  0.676 (0

我相信我似乎无法绕过https://pandas.pydata.org/pandas-docs/stable/generated/pandas.MultiIndex.drop.html函数.

因此,结果数据框应仅包含两个均值列,而不应包含“ std”
非常感谢你.

解决方法:

我认为更好的方法删除所有带有std的列,即MultiIndex的第二级使用参数级别为1的drop

print (df)
              attribute          attribute2          attribute3         
                  test1      std      test2      std      test3      std
d  count type                                                           
r1 10    rx       0.559    (0.0)      0.559    (0.0)      0.568    (0.0)
         sth1     0.653  (0.004)      0.653  (0.004)      0.679  (0.002)
         sth2     0.584  (0.002)      0.584  (0.002)      0.586  (0.003)
         sth3     0.651  (0.005)      0.651  (0.005)      0.676      (0)

df = df.drop('std', axis=1, level=1)
print (df)
              attribute attribute2 attribute3
                  test1      test2      test3
d  count type                                
r1 10    rx       0.559      0.559      0.568
         sth1     0.653      0.653      0.679
         sth2     0.584      0.584      0.586
         sth3     0.651      0.651      0.676

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...