I have a DataFrame as follow:
and now I want group by Username and transform to new DataFrame as:
I using Python 2.7 or 3.6 both OK
Seems like you want an agg, not a transform...
df = df.groupby('Username')\
.agg({'Item' : lambda x: x.tolist(), 'Price' : 'sum'})
If you want a comma separated string of words, it would involve an additional 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 |
+------------+---------+-----------+
', '.join(set(x.tolist()))