I have a data frame as shown below
df:
cust_id product
1 tv
1 phone
2 bat
2 ball
3 bat
4 ball
4 bat
4 tv
4 phone
5 tv
6 bat
7 bat
7 ball
7 tv
7 phone
8 phone
8 tv
from the above I would like to prepare below which basically perform groupby cust_id and one hot encoding on the column product.
Expected output:
product ball bat phone tv
cust_id
1 0 0 1 1
2 1 1 0 0
3 0 1 0 0
4 1 1 1 1
5 0 0 0 1
6 0 1 0 0
7 1 1 1 1
8 0 0 1 1
I have tried following code but that did not work.
try-1:
one_hot = pd.get_dummies(df.groupby('cust_id')['product'])
one_hot
try-2
df.pivot_table(index=['cust_id'],
columns='product',
values='product')
try-3
df1 = (df.sort_values(['cust_id', 'product'])
.groupby('cust_id')['product'].agg(list)
.reset_index(name='product_list')
)
df1