Python 熊猫:将分类列拆分为多个列
设想一个具有以下格式的数据帧:Python 熊猫:将分类列拆分为多个列,python,indexing,pandas,Python,Indexing,Pandas,设想一个具有以下格式的数据帧: id type v1 v2 1 A 6 9 1 B 4 2 2 A 3 7 2 B 3 6 id A_v1 A_v2 B_v1 B_v2 1 6 9 4 2 2 3 7 3 6 我想将此数据帧转换为以下格式: id type v1 v2 1 A 6 9 1 B 4 2 2 A 3
id type v1 v2
1 A 6 9
1 B 4 2
2 A 3 7
2 B 3 6
id A_v1 A_v2 B_v1 B_v2
1 6 9 4 2
2 3 7 3 6
我想将此数据帧转换为以下格式:
id type v1 v2
1 A 6 9
1 B 4 2
2 A 3 7
2 B 3 6
id A_v1 A_v2 B_v1 B_v2
1 6 9 4 2
2 3 7 3 6
有一种优雅的方法吗?您可以使用
set\u index
将type
和id
列移动到索引中,
然后unstack
将type
索引级别移动到列索引中。您不必担心v
值——索引的位置决定了值的排列
结果是一个数据帧,列索引为:
In [181]: df.set_index(['type', 'id']).unstack(['type'])
Out[181]:
v1 v2
type A B A B
id
1 6 4 9 2
2 3 3 7 6
通常,多索引比扁平列索引更可取。
它为您提供了基于type
或v
值选择或操作数据的更好方法
如果希望对列重新排序,使其与所需输出中显示的顺序完全匹配,可以使用df.reindex
:
df = df.reindex(columns=sorted(df.columns, key=lambda x: x[::-1]))
屈服
v1 v2 v1 v2
type A A B B
id
1 6 9 4 2
2 3 7 3 6
A_v1 A_v2 B_v1 B_v2
id
1 6 9 4 2
2 3 7 3 6
如果希望将列索引展平到单个级别,则
df.columns = ['{}_{}'.format(t, v) for v,t in df.columns]
屈服
v1 v2 v1 v2
type A A B B
id
1 6 9 4 2
2 3 7 3 6
A_v1 A_v2 B_v1 B_v2
id
1 6 9 4 2
2 3 7 3 6