I have a customized requirement of splicing values of mutiple columns after group by function. Use ':' as seperator when splice each values and '#' as seperator when splice each record.
My dataframe is like:
type1 type2 id score
A B 123 78
A B 124 89
A C 126 45
A C 231 98
A C 657 92
The result should be like:
type1 type2 result
A B 123:78#124:89
A C 126:45#231:98#657:92
I figured out my way to solve it:
g=df.groupby(['type1','type2'])
final=pd.DataFrame(columns=['type1','type2','result'])
for i,j in g:
j['total']=j.apply(lambda x:x['id']+':'+str(x['score']),axis=1)
final.loc[len(final)]=[item for item in i]+['#'.join(j['total'])]
But this way is a little complex, is there any better and simple way to do it?