Python 熊猫列值到列?

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

Pandas column values to columns?

pythonpandas

提问by Idan Gazit

I've seen a few variations on the theme of exploding a column/series into multiple columns of a Pandas dataframe, but I've been trying to do something and not really succeeding with the existing approaches.

我已经看到关于将一个列/系列分解为 Pandas 数据框的多个列的主题的一些变体,但我一直在尝试做一些事情,但并没有真正成功地使用现有的方法。

Given a DataFrame like so:

给定一个像这样的 DataFrame:

    key       val
id
2   foo   oranges
2   bar   bananas
2   baz    apples
3   foo    grapes
3   bar     kiwis

I want to convert the items in the keyseries into columns, with the valvalues serving as the values, like so:

我想将key系列中的项目转换为列,并将val值作为值,如下所示:

        foo        bar        baz
id
2   oranges    bananas     apples
3    grapes      kiwis        NaN

I feel like this is something that should be relatively straightforward, but I've been bashing my head against this for a few hours now with increasing levels of convolution, and no success.

我觉得这应该是相对简单的事情,但是随着卷积水平的提高,我一直在反对这个问题几个小时,但没有成功。

采纳答案by behzad.nouri

There are a few ways:

有几种方法:

using .pivot_table:

使用.pivot_table

>>> df.pivot_table(values='val', index=df.index, columns='key', aggfunc='first')
key      bar     baz      foo
id                           
2    bananas  apples  oranges
3      kiwis     NaN   grapes

using .pivot:

使用.pivot

>>> df.pivot(index=df.index, columns='key')['val']
key      bar     baz      foo
id                           
2    bananas  apples  oranges
3      kiwis     NaN   grapes

using .groupbyfollowed by .unstack:

使用.groupby后跟.unstack

>>> df.reset_index().groupby(['id', 'key'])['val'].aggregate('first').unstack()
key      bar     baz      foo
id                           
2    bananas  apples  oranges
3      kiwis     NaN   grapes

回答by Zero

You could use set_indexand unstack

你可以使用set_indexunstack

In [1923]: df.set_index([df.index, 'key'])['val'].unstack()
Out[1923]:
key      bar     baz      foo
id
2    bananas  apples  oranges
3      kiwis    None   grapes

Or, a simplified groupby

或者,一个简化的 groupby

In [1926]: df.groupby([df.index, 'key'])['val'].first().unstack()
Out[1926]:
key      bar     baz      foo
id
2    bananas  apples  oranges
3      kiwis    None   grapes