1

I have a data-frame like this :

id  value1 value2
 0     1     1
 1     2     3
 2     1     4
 3     1     5
 4     2     1

i want it to be like this :

id  value1 value2  count
 0     1     1,4,5   3
 1     2     3,1     2
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
welu
  • 329
  • 3
  • 12

2 Answers2

2

Use aggregation by agg with join and size, but is necessary converting column to strings:

tups = [('value2', lambda x: ','.join(x.astype(str))), ('count', 'size')]
df1 = df.groupby('value1')['value2'].agg(tups).reset_index()
print (df1)
   value1 value2  count
0       1  1,4,5      3
1       2    3,1      2

Alternative:

tups = [('value2', ','.join), ('count', 'size')]
df1 = df['value2'].astype(str).groupby(df['value1']).agg(tups).reset_index()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

Something like this could also work:

In [2468]: df.value2 = df['value2'].apply(str)

In [2494]: res = df.groupby('value1')['value2'].apply(lambda x:','.join(x)).reset_index()

In [2498]: res['count'] = df.groupby('value1').size().reset_index()[0]

In [2499]: res
Out[2499]: 
   value1 value2  count
0       1  1,4,5      3
1       2    3,1      2
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58