您可以使用boolean indexing
第一,然後用groupby
適用join
:
df = pd.DataFrame({'Leaf_category_id':[111,111,111,222,333],
'session_id':[1,4,1,2,3],
'product_id':[987,987,741,654,321]},
columns =['Leaf_category_id','session_id','product_id'])
print (df)
Leaf_category_id session_id product_id
0 111 1 987
1 111 4 987
2 111 1 741
3 222 2 654
4 333 3 321
print (df[df.Leaf_category_id == 111]
.groupby('session_id')['product_id']
.apply(lambda x: ','.join(x.astype(str))))
session_id
1 987,741
4 987
Name: product_id, dtype: object
編輯的評論:
print (df.groupby(['Leaf_category_id','session_id'])['product_id']
.apply(lambda x: ','.join(x.astype(str)))
.reset_index())
Leaf_category_id session_id product_id
0 111 1 987,741
1 111 4 987
2 222 2 654
3 333 3 321
或者,如果需要對每個獨特的價值在Leaf_category_id
DataFrame
:
for i in df.Leaf_category_id.unique():
print (df[df.Leaf_category_id == i] \
.groupby('session_id')['product_id'] \
.apply(lambda x: ','.join(x.astype(str))) \
.reset_index())
session_id product_id
0 1 987,741
1 4 987
session_id product_id
0 2 654
session_id product_id
0 3 321
同樣,我可以定義e對所有leaf_category id的 – Shubham
獲取錯誤的功能相同** TypeError:序列項0:預期的字符串,找到numpy.int64 ** – Shubham
什麼是df.dtypes? – jezrael