简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM