Reputation: 85
I have the input :
Date Topic Group Return
1/9/2018 X A a
1/9/2018 Y A a
1/9/2018 Z A a
1/9/2018 K B a
1/9/2018 L B a
1/10/2018 M C b
1/10/2018 N C b
1/10/2018 W A b
How I can have the output below: output
Date Group Topic Return
1/9/2018 A X, Y , Z a
B K,L a
1/10/2018 A W b
C M, N b
Thank you very much
Upvotes: 1
Views: 33
Reputation: 76917
Picking up from @Wen's. To aggregate multiple columns.
In [458]: df.groupby(['Date', 'Group'], sort=False).agg(
{'Topic': ', '.join, 'Return': 'first'})
Out[458]:
Topic Return
Date Group
1/9/2018 A X, Y, Z a
B K, L a
1/10/2018 C M, N b
A W b
Upvotes: 2
Reputation: 323226
IIUC groupby
and agg
df.groupby(['Date','Group']).Topic.agg(','.join).to_frame('Topic')
Out[1270]:
Topic
Date Group
1/10/2018 A W
C M,N
1/9/2018 A X,Y,Z
B K,L
Upvotes: 2