Tony
Tony

Reputation: 51

How to combine multiple rows into single row by adding columns with pandas?

I need to combine multiple rows into a single row with pandas, depending on the column 'hash'

View of my dataframe:

    hash     a      b
0     1      1      6
1     1      2      7
2     1      3      8
3     2      4      9 
4     2      5      10

I want the dataframe to be converted like this:

   hash     a      a1     a3     b     b1     b2  
0    1      1      2      3      6     7      8   
1    2      4      5      nan    9     10     nan 

I have tried to use some code related to groupby or transpose the whole dataframe but cant figure out how to do it. Anyone could help me out?

Upvotes: 0

Views: 555

Answers (1)

jezrael
jezrael

Reputation: 862661

Create MultiIndex by set_index with counter column by cumcount, reshape by unstack and flatten Multiindex by map with join:

df1 = df.set_index(['hash', df.groupby('hash').cumcount().add(1).astype(str)]).unstack()
df1.columns = df1.columns.map(''.join)
df1 = df1.reset_index()
print (df1)
   hash   a1   a2   a3   b1    b2   b3
0     1  1.0  2.0  3.0  6.0   7.0  8.0
1     2  4.0  5.0  NaN  9.0  10.0  NaN

Upvotes: 1

Related Questions