Assume my df is as follows:
df = pd.DataFrame({'Order ID': [1, 1, 2, 3, 3, 3, 4, 4], 'Product': ['USB', 'Bat', 'Ball', 'USB', 'Phone', 'Toy', 'Bike', 'Apple']})
I want to group by Order ID, and then put the Product values in a list of lists depending on the frequency of their Order ID. For example -
[['USB', 'Bat'], ['Ball'], ['USB', 'Phone', 'Toy'], ['Bike', 'Apple']]
USB and Bat both have the same order ID (1), so they are in the same list.
My code, trial 1:
combo_outer = [] # outer most list
grouped = df.groupby(['Order ID', 'Product'])
for group, frame in grouped:
combo_inner = [] # for inner lists
for row_index, row in frame.iterrows():
combo_inner.append(row['Product'])
combo_outer.extend(combo_inner)
Trial 2:
df['Product'].values.tolist()
In both cases, I end up getting a single list:
['Bat', 'USB', 'Ball', 'Phone', 'Toy', 'USB', 'Apple', 'Bike']
What am I doing wrong?