Shares and Percentages of a Total
A group's total means more beside the whole, so divide each group's total by the grand total and multiply by 100 for its share as a percentage; transform('sum') lines each group's total up with every row, so each row's share of its own group is one division; and shares are rounded to one decimal place only at the end, for people to read.
Journeyman · Python · Updated
After this lesson you can
- Work out each group's share of a grand total as a percentage, rounded to one decimal place for people
- Work out each row's share of its own group with transform('sum')
Key takeaways
- A share is a part divided by the whole. by_hood / by_hood.sum() * 100 gives each neighborhood's percentage of all sales.
- Shares of orders and shares of dollars answer different questions, and can differ. Say which one a number is.
- transform("sum") gives every row its group's total, lined up with the table, so each row's share of its own group is one division.
- Round at the end, for people. Rounded shares can add up to 99.9 or 100.1, and that's rounding, not a mistake.
Crust & Co. Pizza's Westgate orders brought in $9,472.29 this summer. Is that a lot? On its own, nobody can tell. Next to the shop's $74,501.23 in all, it's about an eighth of the business, and that's a number the owner can plan with. Shares and percentages turn totals into slices of a whole: a club's budget by category, a survey's answers, a store's sales by region. You'll write them in nearly every report.
The data is the shop's delivery orders, orders.csv: 3,006 orders over 13 weeks, with each order's neighborhood, size, total in dollars and more. Grouping with groupby and Several Summaries at Once: agg make the totals this lesson starts from.
A group's share of the grand total
Start with the total for each neighborhood, and the grand total, the total of everything:
import pandas as pd
orders = pd.read_csv("orders.csv")
by_hood = orders.groupby("neighborhood")["total"].sum()
print("Westgate:", round(by_hood["Westgate"], 2))
print("all neighborhoods:", round(by_hood.sum(), 2))
Westgate: 9472.29
all neighborhoods: 74501.23
A share is the part divided by the whole, and times 100 makes it a percentage. Dividing a column of numbers by one number divides every value in it, so one line works out all five:
share = by_hood / by_hood.sum() * 100
share.round(1).sort_values(ascending=False)
neighborhood
Elm Park 27.4
Hillside 23.2
Old Town 19.1
Lakeshore 17.6
Westgate 12.7
Name: total, dtype: float64
Read the first line as "each neighborhood's sales, divided by all the sales, times 100". Elm Park brings in 27.4% of the money.
Shares of orders, not of dollars
"What share of our orders come from Westgate?" is a different question. Count the rows in each group with size(), and divide by the number of orders:
orders_share = orders.groupby("neighborhood").size() / len(orders) * 100
orders_share.round(1)
neighborhood
Elm Park 27.2
Hillside 22.8
Lakeshore 17.4
Old Town 19.0
Westgate 13.6
dtype: float64
Westgate places 13.6% of the orders but brings in only 12.7% of the money: its orders are smaller, because the shop doesn't deliver party pizzas that far. Both numbers are true, so a report says which share it means. (orders["neighborhood"].value_counts(normalize=True) gives the same shares as fractions of 1, largest first.)
Each row's share of its own group: transform
Now the owner asks, "In each neighborhood, what share of its sales are party pizzas?" The whole is different for every row: Elm Park's party pizzas divide by Elm Park's total, Hillside's by Hillside's. Each row needs its own neighborhood's total beside it.
groupby(...).sum() can't do that: it hands back five rows, one per neighborhood. transform("sum") works out the same sums but hands back one value for every row, each row getting its group's total, lined up with the table:
by_both = orders.groupby(["neighborhood", "size"], as_index=False)["total"].sum()
by_both["neighborhood_total"] = by_both.groupby("neighborhood")["total"].transform("sum")
by_both.head(5)
neighborhood size total neighborhood_total
0 Elm Park large 6457.62 20383.60
1 Elm Park medium 7355.85 20383.60
2 Elm Park party 3432.39 20383.60
3 Elm Park small 3137.74 20383.60
4 Hillside large 5278.00 17301.45
Every Elm Park row has 20383.60 beside it, the Hillside rows 17301.45. The share is now one division, row by row, and keeping only the party rows answers the owner:
by_both["share"] = (by_both["total"] / by_both["neighborhood_total"] * 100).round(1)
by_both[by_both["size"] == "party"]
neighborhood size total neighborhood_total share
2 Elm Park party 3432.39 20383.60 16.8
6 Hillside party 3474.36 17301.45 20.1
10 Lakeshore party 2003.12 13094.84 15.3
14 Old Town party 2371.77 14249.05 16.6
Party pizzas are a fifth of Hillside's sales. Westgate has no row here, because it has no party orders.
Rounded for people
Without round, Elm Park's share prints as 27.360085, more digits than anyone needs and more than the data can promise. One decimal place is the usual choice for a percentage. Round at the end, after dividing: rounding the totals first would make every share a little wrong.
Rounded shares don't always add up to exactly 100. Here the five neighborhoods add up to 100.0, but another table could give 99.9 or 100.1, because each share was rounded on its own. That's rounding, not a mistake; a footnote such as "shares may not add to 100 because of rounding" tells the reader so.
Check yourself
2 questions to try on the lesson page.
So what
A total only means something next to its whole, so divide by the grand total for a group's share, use transform("sum") when each row needs its own group's total, and round once, at the end, for the people who read it.