下面是我拥有的一个数据框的小例子。每个用户都有2行,我想将2行合并为1行。
(原始数据框)
USER DETAIL TEAM VALUE
JohnDoe Disponibily Architect 107
JohnDoe Capacity Architect 240
JamesDean Disponibily Coder 80
JamesDean Capacity Coder 110
我正在尝试通过熊猫实现类似的目标:
USER TEAM Disponibily Capacity
JohnDoe Architect 107 240
JamesDean Coder 80 110
任何帮助将不胜感激。
答案 0 :(得分:2)
df = pd.DataFrame(data={"USER":["JohnDoe","JohnDoe","JamesDean","JamesDean"],
"DETAIL":["Disponibily","Capacity","Disponibily","Capacity"],
"TEAM":["Architect","Architect","Coder","Coder"],
"VALUE":[107,240,80,110]})
res = pd.pivot_table(df,index=['USER','TEAM'],columns='DETAIL',values='VALUE').reset_index()
res.columns.name = ''
USER TEAM Capacity Disponibily
0 JamesDean Coder 110 80
1 JohnDoe Architect 240 107