我有下面的df
customer class score
A a 10
A b 20
B a 40
B b 50
我想像这样分组,变换和计算。
customer score(b-a)
A 10
B 10
我不知道如何计算。
df.groupby(df.customer)
如果有人经历了这种聚集,请告诉我。
谢谢
答案 0 :(得分:4)
您可以使用@HenryYik的评论,也可以使用thread
:
sort()
输出:
pivot
答案 1 :(得分:1)
替代解决方案,按客户分组并应用自定义功能
def get_score(temp):
map_score = dict(zip(temp['class'], temp['score'])) # mapping of class and score for each customer
return map_score['b'] - map_score['a']
df.groupby("customer").apply(get_score)
这将导致预期的答案。