Pivot table with list entries pandas data frame

This is str.get_dummies then groupby:

(df['products_purchased'].str.get_dummies(',')
   .groupby(df['customer_id']).sum()
   .reset_index()
)

Output:

   customer_id  A  B  C  D  H  K  M  P  Q  R  T  U  Z
0            1  2  1  1  1  0  0  0  0  1  0  0  1  0
1            2  0  1  0  0  0  1  0  0  0  0  1  0  0
2            3  1  0  0  0  0  0  0  1  0  0  1  0  0
3            4  0  0  0  0  1  0  1  0  0  1  1  1  1

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top