Combining Groupby and Join
Example of Using Groupby and Join¶
You can combine this with a join to do something like find the percent of daily sales that each row is contributing. The steps are the following:
- Find the total sales by day
In [5]:
#Find the daily sales
daily_sales = sales.groupby('Day')['Sales'].sum()
print(daily_sales)
- Give this series a name to be called when we join it.
In [6]:
#Give the name to the series
daily_sales.name = "Total Daily Sales"
print(daily_sales)
- Join this data into our main dataset. Keep in mind that daily_sales has an index of day, but there is no index for our main data. Instead of setting the index on our main data we can use the keyword on and give the column to use as the index.
In [7]:
#Join the daily sales data
sales = sales.join(daily_sales, on="Day")
print(sales)
- Create a new column which divides sales by total daily sales.
In [8]:
sales['Percent of Daily Sales'] = sales['Sales'] / sales['Total Daily Sales']
print(sales)