S Ringne S Ringne - 19 days ago 5
Python Question

Function to select from columns pandas df

i have this test table in pandas dataframe

Leaf_category_id session_id product_id
0 111 1 987
3 111 4 987
4 111 1 741
1 222 2 654
2 333 3 321


enter image description here

what i want is

for leaf_category_id 111:


the result should be.

session_id product_id
1 987,741
4 987


Similarly can i define a function that does the same for all the leaf_category id's, my table contains more rows, it was just a snapshot of it.

Answer

You can use boolean indexing first and then groupby with apply 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

EDIT by comment:

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

Or if need for each unique value in 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
Comments