Python Pandas:从多级列索引中删除一列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25135578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Pandas: drop a column from a multi-level column index?
提问by Boosted_d16
I have a multi level column table like this:
我有一个像这样的多级列表:
a
---+---+---
b | c | f
--+---+---+---
0 | 1 | 2 | 7
1 | 3 | 4 | 9
How can I drop column "c" by name? to look like this:
如何按名称删除列“c”?看起来像这样:
a
---+---
b | f
--+---+---
0 | 1 | 7
1 | 3 | 9
I tried this:
我试过这个:
del df['c']
but I get the following error, which makes sense:
但我收到以下错误,这是有道理的:
KeyError: 'Key length (1) was greater than MultiIndex lexsort depth (0)'
KeyError: '键长度 (1) 大于 MultiIndex 词法排序深度 (0)'
采纳答案by Boosted_d16
Solved:
解决了:
df.drop('c', axis=1, level=1)
回答by Mint
With a multi-index we have to specify the column using a tuple in order to drop a specific column, or specify the level to drop all columns with that key on that index level.
对于多索引,我们必须使用元组指定列以删除特定列,或者指定级别以删除该索引级别上具有该键的所有列。
Instead of saying drop column 'c'say drop ('a','c')as shown below:
而不是说 drop column 'c'说 drop ('a','c')如下所示:
df.drop(('a', 'c'), axis = 1, inplace = True)
Or specify the level as shown below
或指定级别如下图
df.drop('c', axis = 1, level = 1)
Let's make a simple df to demonstrate on:
让我们做一个简单的 df 来演示:
>>> cols = pd.MultiIndex.from_tuples([("a", "b"), ("a", "c"),
... ("a", "f"), ('x', 'c'),('x', 'f')])
>>> df = pd.DataFrame([[1,3, 7, 21, 8], [2, 4, 9, 21, 8]], columns=cols)
>>> df
a x
b c f c f
0 1 3 7 21 8
1 2 4 9 21 8
Now here's how to drop 'c' from 'a'
现在这里是如何从 'a' 中删除 'c'
>>> df.drop(('a', 'c'), axis = 1)
a x
b f c f
0 1 7 21 8
1 2 9 21 8
With a three level index then include that key in the tuple to drop from the bottom level e.g. ('a','c','k')
使用三级索引,然后将该键包含在元组中以从底层删除,例如('a','c','k')
With a single value as the index, like you did, it searches the top level index for a match by default and drops a match on that index or throws an error if the key is not in the index, like you found.
使用单个值作为索引,就像您所做的那样,默认情况下它会搜索顶级索引以查找匹配项,并在该索引上删除匹配项,或者如果键不在索引中,则抛出错误,就像您发现的那样。
So in my example it would be fine to tell it to drop just 'x'
所以在我的例子中,告诉它只删除“x”就可以了
>>> df.drop('x', axis = 1)
a
b c f
0 1 3 7
1 2 4 9
To drop all columns with the second index 'c', then specify the level
要删除具有第二个索引“c”的所有列,然后指定级别
>>> df.drop('c', axis = 1, level = 1)
a x
b f f
0 1 7 8
1 2 9 8

