There is a df with 2 columns
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
Need to combine properties_ids rows into the list of integers for each group. The other words, desired output for each group_id 3588 [1,2,3,4,5,6]
, 3589 [1,2,3]
etc. To get it I use self written combine function based on concatenation via ','.join
. The result isn't what I expected to get. Cannot understand the behavior of result
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]
Using df.groupby('goods_id')['properties_id'].apply(combine)
gives me the TypeError: sequence item 0: expected str instance, int found