Pandas - 使用将 2 行合并为 1 行 [英] Pandas - Merging 2 rows into 1 using

查看:79
本文介绍了Pandas - 使用将 2 行合并为 1 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我拥有的数据框的一个小例子.每个用户有 2 行,我想将 2 行合并为 1 行.

Below a small example of a dataframe that i have. Every User have 2 rows and i want to merge the 2 into 1 row.

(原始数据框)

USER       DETAIL          TEAM         VALUE
JohnDoe    Disponibily     Architect    107
JohnDoe    Capacity        Architect    240
JamesDean  Disponibily     Coder        80
JamesDean  Capacity        Coder        110

我正在尝试用熊猫来实现这样的目标:

I'm trying to achieve something like this with pandas :

USER       TEAM            Disponibily  Capacity
JohnDoe    Architect       107          240
JamesDean  Coder           80           110

任何帮助将不胜感激.

推荐答案

使用 数据透视表

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

这篇关于Pandas - 使用将 2 行合并为 1 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆