Skip to content
Advertisement

How to create a list from dataframe pandas

My dataset contains columns of usersID and itemsID that they have purchased. Each user might have purchased more than 1 item.

I neeed to make a list so that the key will be the userID and the values the itemsID he purchased for example if user_1 has purchased [item_20,item_25,item_32], my dataset contains 3 rows for this user as follows

row_1= 1,20 row_2= 1,25 row_3= 1,32

I want my list to have the fromat {1: [20,25,32]}

I want to creat a list for all the users in my dataset as the example above.

Advertisement

Answer

if I understand correctly you want something like this!

Next time it would help to see what things you have tried

df = pd.DataFrame({'user': ['K0', 'K0', 'K2', 'K3', 'K4', 'K5'],
                   'product': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})

my_final_list={}
grouped_df=df.groupby(by=["user"])

for key, item in grouped_df:
    products_list=list(grouped_df.get_group(key)["product"])
    my_final_list[key]=products_list

print(my_final_list)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement