dict_ = {
'stu':['a','b','a','b','c'],
'fav':['fa','fb','faa','fbb','fc']
}
df_ = pd.DataFrame(dict_)
print(df_)
#输出内容:
# 通过 groupby apply 加lambda实现group_contact
print(df_.groupby('stu').apply(lambda x: ','.join(x.fav)))
当然这样也可以实现:
print(df_.groupby('stu').apply(lambda x: list(x.fav)))
或则:
print(df_.groupby('stu').agg({'fav' : lambda x: ', '.join(x)}))