我有两列的数据框:user和lang。每个用户都知道一种或多种语言: lang user0 Python Mike1 Scala Mike2 R John3 Julia Michael4 Java Michael我需要用user他/她知道的所有语言获取每一行。我可以这样做:df.groupby('user')['lang'].apply(lambda x:', '.join(x)).reset_index()但我明白了: user lang0 John R1 Michael Julia, Java2 Mike Python, Scala而不是我想要的: lang user0 Python,Scala Mike1 Python,Scala Mike2 R John3 Julia,Java Michael4 Julia,Java Michael重现代码:import pandas as pddf = pd.DataFrame({"lang":["Python","Scala","R","Julia","Java"], "user":["Mike","Mike","John","Michael","Michael"]})print(df)
1 回答
蝴蝶刀刀
TA贡献1801条经验 获得超8个赞
用于transform将groupby结果“广播”到输入中的每一行。
df['lang'] = df.groupby('user')['lang'].transform(', '.join)
df
lang user
0 Python, Scala Mike
1 Python, Scala Mike
2 R John
3 Julia, Java Michael
4 Julia, Java Michael
添加回答
举报
0/150
提交
取消