Multi-index Grouping
Multi-index Grouping¶
You are not limited to just using a single column for grouping, you can also pass in a list to do the grouping based on multiple groupings. Let’s take an example by bringing back the original dataset again. One thing to keep in mind is you will get back data with a multi-index based on these groupings.
In [9]:
#Create the data
sales = pd.DataFrame([[1, 1, 1, 10],
[1, 1, 2, 74],
[1, 1, 3, 27],
[1, 2, 1, 41],
[1, 2, 2, 66],
[1, 2, 3, 95],
[2, 1, 1, 1],
[2, 1, 2, 23],
[2, 1, 3, 67],
[2, 2, 1, 86],
[2, 2, 2, 87],
[2, 2, 3, 19],
[3, 1, 1, 6],
[3, 1, 2, 30],
[3, 1, 3, 55],
[3, 2, 1, 68],
[3, 2, 2, 32],
[3, 2, 3, 50]],
columns=['Day', 'Store', 'Product', 'Sales'])
print(sales)
Now what if we wanted to find the total sales by day and product? We can use both of them in grouping like below:
In [10]:
#Group by day and product
group_data = sales.groupby(['Day', 'Product'])['Sales'].sum()
print(group_data)
You might also want something like average sales for each store-day combo.
In [11]:
group_data = sales.groupby(['Store', 'Day'])['Sales'].mean()
print(group_data)