3

Given a Pandas Dataframe df, with column names 'Session', and 'List':

Can I group together the 'List' values for the same values of 'Session'?

My Approach

I've tried solving the problem by creating a new dataframe, and iterating through the rows of the inital dataframe while maintaing a session counter that I increment if I see that the session has changed.

If it hasn't changed, then I append the List value that corresponds to that rows value with a comma.

Whenever the session changes, I used strip to get rid of the last comma (extra).

Initial DataFrame

   Session  List  
0     1      a    
1     1      b    
2     1      c     
3     2      d     
4     2      e    
5     3      f     

Required DataFrame

   Session  List  
0     1      a,b,c   
1     2      d,e  
2     3      f     

Can someone suggest something more efficient or simple?

Thank you in advance.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
user123456789
  • 31
  • 1
  • 2

1 Answers1

4

Use groupby and apply and reset_index:

>>> df.groupby('Session')['List'].agg(','.join).reset_index()
   Session   List
0        1  a,b,c
1        2    d,e
2        3      f
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114