Python 大熊猫中的拆垛
与一个不稳定的问题作斗争。我有以下格式的表格:Python 大熊猫中的拆垛,python,pandas,stack,pivot,Python,Pandas,Stack,Pivot,与一个不稳定的问题作斗争。我有以下格式的表格: Word Number MetaData Label Value One 1 Hello A 5a One 1 Hello B 2b One 1 Hello C 8c Two 2 World A 2a Two 2 World
Word Number MetaData Label Value
One 1 Hello A 5a
One 1 Hello B 2b
One 1 Hello C 8c
Two 2 World A 2a
Two 2 World B 5b
Two 2 World C 1c
我想把它拆散到这里,保留我所有的其他专栏。在所有情况下,每组A、B和C的字、数字和元数据始终相同:
Word Number MetaData A B C
One 1 Hello 5a 2b 8c
Two 2 World 2a 5b 1c
您想在这里查看透视表:
df.pivot_table(
index=['Word', 'Number', 'MetaData'],
columns='Label',
values='Value'
).reset_index()
你需要的是旋转,而不是拆垛。我对问题进行了编辑,以便更清楚。Pivot似乎暗示我在总结值字段。我正在做的最接近的例子是一个jmp unstack函数,同时保留我的所有列。我将重新提交问题和我的编辑。
Label Word Number MetaData A B C
0 One 1 Hello 5 2 8
1 Two 2 World 2 5 1