python pandas dataframe 实现mysql group_contact功能
作者:互联网
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)}))
标签:group,stu,python,fav,dataframe,df,print,groupby,lambda 来源: https://www.cnblogs.com/twain/p/14720760.html