【发布时间】:2021-02-05 14:00:53
【问题描述】:
我有以下数据框。
| Fruit | Description |
|---|---|
| Apple | ["red", "big"] |
| Banana | ["yellow", "long"] |
| Banana | ["elongated, twisted"] |
| Peach | ["round"] |
| Apple | ["round", "greenish"] |
我正在尝试通过列表的串联,根据水果的描述进行分组。 我应该得到:
| Fruit | Description |
|---|---|
| Apple | ["red", "big", "round", "greenish"] |
| Banana | ["yellow", "long", "elongated, twisted"] |
| Peach | ["round"] |
我遵循了此处提供的解决方案:pandas groupby and join lists:
df = df.groupby('Fruit', as_index=False).agg(Description =('Description', 'sum'))
但我得到的是相互关联的列表:
| Fruit | Description |
|---|---|
| Apple | ["red", "big"]["round", "greenish"] |
| Banana | ["yellow", "long"]["elongated, twisted"] |
| Peach | ["round"] |
有人有解决办法吗? 谢谢!
【问题讨论】:
-
您的
Description值似乎是字符串,而不是列表。你检查过它的数据类型吗? -
确实如此。
标签: python pandas pandas-groupby