-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.

(Original dataframe)

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

Any help would be greatly appreciated.

Mooh
  • 1,237
  • 3
  • 19
  • 38
  • 1
    Use unstack, like: `df.set_index(['USER', 'TEAM', 'DETAIL'], inplace=True)` then `df.unstack(-1)`. Actually I was just writing an answer, when the question was marked as duplicate. – jottbe Jul 22 '19 at 16:10
  • Please place your code in the question, so others can reproduce/solve the problem faster. – adnanmuttaleb Jul 22 '19 at 16:10

1 Answers1

2

use pivot_table

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
tawab_shakeel
  • 3,701
  • 10
  • 26