如何按列值分组后,将字符串连成?

摘要:In[38]: df.groupby(['name','month'])['text'].app
In[38]: df.groupby(['name','month'])['text'].apply(','.join).reset_index() Out[38]: name month text 0 name1 11 du 1 name1 12 aj,oj 2 name2 11 fin,katt 3 name2 12 mycket,lite groupby 进行分组 [‘name’, 'month']是分组的字段 [‘text’]是需要连接的列名 apply对text应用对应的处理函数 reset_index 合并之后需要重新重置index 参考来源: https://stackoverflow.com/questions/27298178/concatenate-strings-from-several-rows-using-pandas-groupby