【发布时间】:2023-01-26 18:11:17
【问题描述】:
我有这张桌子:
| id | type | text |
|---|---|---|
| 1 | inv_num | 123 |
| 1 | company | ASD |
| 1 | item | fruit |
| 1 | item | vegetable |
| 2 | inv_num | 123 |
| 2 | company | FOO |
| 2 | item | computer |
| 2 | item | mouse |
| 2 | item | headphones |
我想以列表格式将相同类型分组在一行中:
| id | type | text |
|---|---|---|
| 1 | inv_num | 123 |
| 1 | company | ASD |
| 1 | item | ['fruit', 'vegetable'] |
| 2 | inv_num | 123 |
| 2 | company | FOO |
| 2 | item | ['computer', 'mouse', 'headphones'] |
是否可以使用“groupby”来做到这一点?
【问题讨论】:
-
如果您只需要包含多个项目的组的列表:使用
lambda x: list(x) if len(x)>1 else x作为聚合函数