Python Pandas:从多级列索引中删除一列? [英] Python Pandas: drop a column from a multi-level column index?

查看:1061
本文介绍了Python Pandas:从多级列索引中删除一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的多级列表:

I have a multi level column table like this:

    a
   ---+---+---
    b | c | f
--+---+---+---
0 | 1 | 2 | 7
1 | 3 | 4 | 9

如何按名称删除列 c?看起来像这样:

How can I drop column "c" by name? to look like this:

    a
   ---+---
    b | f
--+---+---
0 | 1 | 7
1 | 3 | 9

我尝试过:

del df['c']

但出现以下错误,这很有意义:

but I get the following error, which makes sense:


KeyError:'密钥长度(1)大于MultiIndex lexsort深度(0)'

KeyError: 'Key length (1) was greater than MultiIndex lexsort depth (0)'


推荐答案

已解决:

df.drop('c', axis=1, level=1)

这篇关于Python Pandas:从多级列索引中删除一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆