Pandas 将int64合并到表中groupby之后的整数列表中

Pandas 将int64合并到表中groupby之后的整数列表中,pandas,python-3.5,Pandas,Python 3.5,有一个带有2列的df goods_id int64 properties_id int64 dtype: object df goods_id properties_id 0 3588 1 1 3588 2 2 3588 3 3 3588 4 4 3588 5 5 3588 6 6 3589 1 7 3589 2

有一个带有2列的df

goods_id         int64
properties_id    int64
dtype: object

df
      goods_id  properties_id
    0   3588    1
    1   3588    2
    2   3588    3
    3   3588    4
    4   3588    5
    5   3588    6
    6   3589    1
    7   3589    2
    8   3589    3
需要将属性\u id行合并到每个组的整数列表中。换句话说,每个组的所需输出_id
3588[1,2,3,4,5,6]
3589[1,2,3]
等。要获得它,我使用基于通过
,'串联的自写组合函数。join
。结果不是我所期望的。无法理解结果的行为

def combine(x):
    return ','.join(x)

df.groupby('goods_id').apply(combine)

goods_id
3588    goods_id,properties_id # desired output [1,2,3,4,5,6]
3589    goods_id,properties_id # desired output [1,2,3]
使用
df.groupby('goods\u id')['properties\u id'].apply(combine)
为我提供了
TypeError:sequence item 0:expected str instance,int found
在一行中:

df.groupby('goods_id').agg(lambda col: col.tolist()).reset_index()
提供以下数据帧:

   goods_id       properties_id
0      3588  [1, 2, 3, 4, 5, 6]
1      3589           [1, 2, 3]
如果数据框中有更多的列,它们也将聚合到列表中。如果是这种情况,并且您只希望
properties\u id
成为列表,则只需在
.agg()
中指定此列:


谢谢,一切都很好。但是为什么我的方法会通过聚合列名而不是它们的值给出意外的结果呢?几分钟后我就能得到答案
df.groupby('goods_id').agg({'properties_id': lambda col: col.tolist()}).reset_index()