【问题标题】:Opposite of melt in python pandaspython pandas中融化的对面
【发布时间】:2014-04-03 08:50:10
【问题描述】:

我不知道如何在 python 中使用 Pandas 进行“反向融化”。 这是我的起始数据

import pandas as pd

from StringIO import StringIO

origin = pd.read_table(StringIO('''label    type    value
x   a   1
x   b   2
x   c   3
y   a   4
y   b   5
y   c   6
z   a   7
z   b   8
z   c   9'''))

origin
Out[5]: 
  label type  value
0     x    a      1
1     x    b      2
2     x    c      3
3     y    a      4
4     y    b      5
5     y    c      6
6     z    a      7
7     z    b      8
8     z    c      9

这是我想要的输出:

    label   a   b   c
        x   1   2   3
        y   4   5   6
        z   7   8   9

我确信有一种简单的方法可以做到这一点,但我不知道如何。

【问题讨论】:

  • Docstring of melt: "Unpivots" 一个 DataFrame... :)
  • StringIO 已移至 python3 中的io。使用from io import StringIOpython3.
  • 我在Q&A中提供了几个详细的示例和替代方法

标签: python pandas pivot reshape melt


【解决方案1】:

有几种方法;
使用.pivot:

>>> origin.pivot(index='label', columns='type')['value']
type   a  b  c
label         
x      1  2  3
y      4  5  6
z      7  8  9

[3 rows x 3 columns]

使用pivot_table:

>>> origin.pivot_table(values='value', index='label', columns='type')
       value      
type       a  b  c
label             
x          1  2  3
y          4  5  6
z          7  8  9

[3 rows x 3 columns]

.groupby 后跟.unstack

>>> origin.groupby(['label', 'type'])['value'].aggregate('mean').unstack()
type   a  b  c
label         
x      1  2  3
y      4  5  6
z      7  8  9

[3 rows x 3 columns]

【讨论】:

  • 太棒了!我现在想把它变成一个简单的字典,索引列也来了。该怎么做?
  • 以上哪一项最通用?如果不是有一个单一的值列,而是有很多 - 通常会使用哪个? (枢轴?)
【解决方案2】:

DataFrame.set_index + DataFrame.unstack

df.set_index(['label','type'])['value'].unstack()

type   a  b  c
label         
x      1  2  3
y      4  5  6
z      7  8  9

简化枢轴参数的传递

df.pivot(*df)

type   a  b  c
label         
x      1  2  3
y      4  5  6
z      7  8  9

[*df]
#['label', 'type', 'value']

对于预期输出,我们需要 DataFrame.reset_indexDataFrame.rename_axis

df.pivot(*df).rename_axis(columns = None).reset_index()

  label  a  b  c
0     x  1  2  3
1     y  4  5  6
2     z  7  8  9

如果a,b 列中有重复项,我们可能会丢失信息,因此我们需要GroupBy.cumcount

print(df)

  label type  value
0     x    a      1
1     x    b      2
2     x    c      3
3     y    a      4
4     y    b      5
5     y    c      6
6     z    a      7
7     z    b      8
8     z    c      9
0     x    a      1
1     x    b      2
2     x    c      3
3     y    a      4
4     y    b      5
5     y    c      6
6     z    a      7
7     z    b      8
8     z    c      9

df.pivot_table(index = ['label',
                        df.groupby(['label','type']).cumcount()],
               columns = 'type',
               values = 'value')


type     a  b  c
label           
x     0  1  2  3
      1  1  2  3
y     0  4  5  6
      1  4  5  6
z     0  7  8  9
      1  7  8  9

或者:

(df.assign(type_2 = df.groupby(['label','type']).cumcount())
   .set_index(['label','type','type_2'])['value']
   .unstack('type'))

【讨论】:

    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    相关资源
    最近更新 更多