答案 0 :(得分:1)
好像你想要agg
,而不是transform
......
df = df.groupby('Username')\
.agg({'Item' : lambda x: x.tolist(), 'Price' : 'sum'})
如果您想要逗号分隔的字词串,则会涉及额外的str.join
:
df = df.groupby('Username')\
.agg({'Item' : lambda x: ', '.join(x.tolist()), 'Price' : 'sum'})
df
+------------+---------+-----------+
| Username | Price | Item |
|------------+---------+-----------|
| Johne | 4500 | Food, Tea |
| Mary | 1000 | Meat |
| Peter | 2850 | Food, Egg |
| Ted | 750 | CK |
+------------+---------+-----------+