我有一张桌子,如下所示:
Id Family Modal
a1 Jack A381
a2 Jack B674
a4 Sutyama 789b
a5 Sutyama 987y
我希望得到以下输出
Id Family Modal Overall
a1 Jack A381 A381,B674
a2 Jack B674 A381,B674
a4 Sutyama 789b 789b,987y
a5 Sutyama 987y 789b,987y
我尝试下面的代码,但返回的空列是总体
df["Overall"]=df.groupby("Family")["Modal"].apply(' '.join)
有人有想法吗?
答案 0 :(得分:3)
在使用groupby
应用函数时,这是我的经验法则:
GroupBy.agg
或GroupBy.apply
,或GroupBy.transform
。这是第二条规则的用例:
df['Overall'] = df.groupby("Family")["Modal"].transform(','.join)
df
Id Family Modal Overall
0 a1 Jack A381 A381,B674
1 a2 Jack B674 A381,B674
2 a4 Sutyama 789b 789b,987y
3 a5 Sutyama 987y 789b,987y
答案 1 :(得分:2)
您还可以通过map
df["Overall"]=df.Family.map(df.drop_duplicates(['Family','Modal']).groupby("Family")["Modal"].apply(' '.join))
df
Out[45]:
Id Family Modal Overall
0 a1 Jack A381 A381 B674
1 a2 Jack B674 A381 B674
2 a4 Sutyama 789b 789b 987y
3 a5 Sutyama 987y 789b 987y