微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐