我有两列的数据框:user和lang。每个用户都知道一种或多种语言:
lang user
0 Python Mike
1 Scala Mike
2 R John
3 Julia Michael
4 Java Michael
我需要用user他/她知道的所有语言获取每一行。我可以这样做:
df.groupby('user')['lang'].apply(lambda x:', '.join(x)).reset_index()
但我明白了:
user lang
0 John R
1 Michael Julia, Java
2 Mike Python, Scala
而不是我想要的:
lang user
0 Python,Scala Mike
1 Python,Scala Mike
2 R John
3 Julia,Java Michael
4 Julia,Java Michael
重现代码:
import pandas as pd
df = pd.DataFrame({"lang":["Python","Scala","R","Julia","Java"],
"user":["Mike","Mike","John","Michael","Michael"]})
print(df)
蝴蝶刀刀
相关分类