Skip to content Skip to sidebar Skip to footer

Extracting Tuples From A List In Pandas Dataframe

I have a dataframe with 12 column. I would like to extract the rows of a column depending on the values of another column. Sample of my dataframe order_id order_type order_ite

Solution 1:

IIUC, try using pandas.DataFrame.groupby after evaluation:

my_dict = df.groupby('order_type')['order_items'].apply(lambda x: sum(x, [])).to_dict()
print(my_dict)

Output:

{'Breakfast': [('Coffee', 2), ('Eggs', 3)],
 'Dinner': [('Shrimp', 10), ('Fish&Chips', 7)],
 'Lunch': [('Burger', 5), ('Fries', 6), ('Salad', 9), ('Steak', 9)]}

Post a Comment for "Extracting Tuples From A List In Pandas Dataframe"