How to collapse rows to columns in Pandas?

Question:

I am trying to collapse a dataframe of multiple rows in pandas.

Let’s say the dataframe looks like this

ID      Key  Value  Object
1001    K    1        4
1001    C    2        7
1001    D    3        9
2001    K    1        19
2001    C    2        10

Now I would like to collapse the dataframe to the following:

ID   Value_K   Value_C   Value_D  Object_K  Object_C Object_D
1001   1         2         3         4         7        9
2001   1         2         NA        19        10       NA

Could someone be so kind to help me? I have tried with pivot(), pivot_table(), collapse(), and melt() but have not achieved any results.

Asked By: raffaelo92

||

Answers:

You can use groupby and unstack

df = pd.DataFrame({'ID': [1001,1001,1001,2001,2001], 'Key': ['K', 'C', 'D', 'K', 'C'], 'Value': [1,2,3,1,2],
                  'Object': [4,7,9,19,10]})
df = df.groupby(['ID', 'Key']).sum().unstack()
df.columns = df.columns.map('{0[0]}-{0[1]}'.format)
df = df.reset_index()
print(df)

     ID  Value-C  Value-D  Value-K  Object-C  Object-D  Object-K
0  1001      2.0      3.0      1.0       7.0       9.0       4.0
1  2001      2.0      NaN      1.0      10.0       NaN      19.0
Answered By: heena bawa
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.