Python 通过将两列分组来重新组织数据帧
我正在尝试转换以下格式的数据帧:Python 通过将两列分组来重新组织数据帧,python,pandas,dataframe,Python,Pandas,Dataframe,我正在尝试转换以下格式的数据帧: file config value name1 a 123 name1 b 123 name1 c 123 name2 a 456 name2 b 789 name2 c 123 如有以下格式: file a b c name1 123 123 123 name2 456 789 123 我希望我的'config'值成
file config value
name1 a 123
name1 b 123
name1 c 123
name2 a 456
name2 b 789
name2 c 123
如有以下格式:
file a b c
name1 123 123 123
name2 456 789 123
我希望我的'config'值成为与每个文件的'value'列的值相等的列
关于如何实现这一点,有什么提示吗?那是
pivot\u table
df.pivot_table(index='file', columns='config', values='value')
config a b c
file
name1 123 123 123
name2 456 789 123
这是透视表
df.pivot_table(index='file', columns='config', values='value')
config a b c
file
name1 123 123 123
name2 456 789 123
取消堆叠:
df.set_index(['file', 'config']).unstack()
value
config a b c
file
name1 123 123 123
name2 456 789 123
取消堆叠:
df.set_index(['file', 'config']).unstack()
value
config a b c
file
name1 123 123 123
name2 456 789 123