pandas 将多索引的一级拆分为列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35427002/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:42:27  来源:igfitidea点击:

Split one level of an multi index into columns

pythonpandas

提问by binnisb

So I have a data frame:

所以我有一个数据框:

df = pd.DataFrame([["foo","fizz",1],["foo","fizz",2],["foo","buzz",3],["foo","buzz",4],["bar","fizz",6],["bar","buzz",8]],columns=["a","b","c"])

       a    b     c
    0  foo  fizz  1
    1  foo  fizz  2
    2  foo  buzz  3
    3  foo  buzz  4
    4  bar  fizz  6
    5  bar  buzz  8

That I can group:

我可以分组:

df2 = df.groupby(["a","b"]).sum()

              c
    a   b
    bar buzz  8
        fizz  6
    foo buzz  7
        fizz  3

Which is awesome! But what I really need, instead of the "c" column is two columns, "foo" and "bar":

这太棒了!但我真正需要的是两列,而不是“c”列,“foo”和“bar”:

          foo  bar
    b
    buzz  7    8
    fizz  3    6

Can someone suggest a way to do this? I tried searching, but I guess I don't have the correct terminology for this so I couldn't find anything.

有人可以建议一种方法吗?我尝试搜索,但我想我没有正确的术语,所以我找不到任何东西。

采纳答案by Anton Protopopov

You could use unstackfor that:

你可以使用unstack

df2.unstack(level='a')

Example:

例子:

In [146]: df2.unstack(level='a')
Out[146]:
       c
a    bar foo
b
buzz   8   7
fizz   6   3

After that you'll get multiindexed columns. If you need to get flat dataframe you could use droplevelof multiindex:

之后,您将获得多索引列。如果您需要获得平面数据框,您可以使用droplevel多索引:

df3 = df2.unstack(level='a')
df3.columns = df3.columns.droplevel()

In [177]: df3
Out[177]:
a     bar  foo
b
buzz    8    7
fizz    6    3

EDIT

编辑

dropleveldrops level from MultiIndex which your columns become after unstack. By default it drops level 0 which is what you need for that dataframe.

droplevel从 MultiIndex 降低您的列在 之后的级别unstack。默认情况下,它会降低级别 0,这是该数据帧所需的级别。

Copy from help(pd.core.index.MultiIndex.droplevel):

复制自help(pd.core.index.MultiIndex.droplevel)

Help on function droplevelin module pandas.core.index:

droplevel(self, level=0) Return Index with requested level removed. If MultiIndex has only 2 levels, the result will be of Index type not MultiIndex.

Parameters
----------
level : int/level name or list thereof

Notes
-----
Does not check if result index is unique or not

Returns
-------
index : Index or MultiIndex

模块 pandas.core.index 中函数droplevel 的帮助:

droplevel(self, level=0) 返回已删除请求级别的索引。如果 MultiIndex 只有 2 个级别,则结果将是 Index 类型而不是 MultiIndex。

Parameters
----------
level : int/level name or list thereof

Notes
-----
Does not check if result index is unique or not

Returns
-------
index : Index or MultiIndex