通过分组两列来重新组织数据框

时间:2018-08-04 16:40:50

标签: 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'值成为与每个文件的'value'列等效的列。

关于如何实现这一目标的任何提示?

2 个答案:

答案 0 :(得分:2)

那是pivot_table

df.pivot_table(index='file', columns='config', values='value')

config    a    b    c
file                 
name1   123  123  123
name2   456  789  123

答案 1 :(得分:1)

拆栈:

df.set_index(['file', 'config']).unstack()

       value          
config     a    b    c
file                  
name1    123  123  123
name2    456  789  123
相关问题