【发布时间】: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