Ваш код работает со столбцом Name
, поэтому столбец text
опущен:
#group column name by column age
result = df["name"].groupby(df['age']).agg(','.join).to_frame()
#more common altearnative - group by column age columns specified in [] after groupby()
result = df.groupby('age')['name'].agg(','.join).to_frame()
print (result)
name
age
21 Mike
22 Jow,Piter
25 David
result = df.groupby('age')['text'].agg(','.join).to_frame()
print (result)
text
age
21 aaa
22 bbb,ccc
25 ddd
#if need specified multiple columns
result = df.groupby('age')['name','text'].agg(','.join)
print (result)
name text
age
21 Mike aaa
22 Jow,Piter bbb,ccc
25 David ddd
#if omit [] proceses all non numeric columns
result = df.groupby('age').agg(','.join)
print (result)
name text
age
21 Mike aaa
22 Jow,Piter bbb,ccc
25 David ddd