0

I need to squish multiple rows into one row across multiple columns in pandas.

For example,

df = pd.DataFrame({'A':[1,2,1,1,2,3], 'B':['b1','b2','b3','b4','b5','b6'], 'C':['c','c','c','c','c','c'], 'D':['d1','d2','d3','d4','d5','d6']})

print(df)
   A   B  C   D
0  1  b1  c  d1
1  2  b2  c  d2
2  1  b3  c  d3
3  1  b4  c  d4
4  2  b5  c  d5
5  3  b6  c  d6

here 'A' is the identifier to be used as reference for grouping rest of the rows across the columns and I'd like to convert the dataframe into the following format,

print(df)
   A   B        C   D
0  1  b1,b3,b4  c  d1,d3,d4
1  2  b2,b5     c  d2,d5
2  3  b6        c  d3
Aman Singh
  • 1,111
  • 3
  • 17
  • 31

1 Answers1

2

With groupby

df.groupby(['A', 'C'], as_index=False).agg(','.join)

enter image description here

phi
  • 10,572
  • 3
  • 21
  • 30