…
…
Draw the distribution in a population table of 81 provinces, choose the center and spread by shape, compare regions with box plots, place the extreme province with a z-score and write a six-sentence description for the editor.
You are an intern at the data desk of a news site. The editor asks: "How are our provinces distributed by population, and can we call one of them the 'average province' in a single sentence?" In this task you set Bazaar aside and combine what you learned in lessons 11–17 on a table you have not seen before. There is no new concept; the only new thing is that you now decide for yourself which summary to report.
Data: provinces.csv, the 81 provinces of a fictional country; each row is one province.
| Column | Description |
|---|---|
province | Name of the province |
region | One of seven regions (Coast, Forest, Highlands, Lakes, Mountains, Plains, Steppe) |
population | Population of the province (people) |
area_km2 | Area (km²) |
median_age | Median age of the province's people (years) |
avg_household_size | Average number of people living in a household |
Source and license: Fictional sample data. Hands-on Mentor generated the table; the province names, regions and numbers do not describe a real country. The unit of observation is the province, so the table has no personal data.
Lessons used: reading CSV files, filtering, sorting and grouping (4–9), histogram and frequency table (11), mean and median (12), weighted average (13), standard deviation (14), quartiles, IQR and box plot (15), shape and z-score (16), choosing a chart and honest axes (17).
The cells share one session: each step works with df and the variables defined by the step before it. This task does not lock your progress; it is a recommended stop at the end of the unit.
What you'll do
provinces.csvDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
You are at the data desk of a news site. The editor asks: "How are our provinces distributed by population, and can one sentence about the 'average province' do?" The table is provinces.csv: the 81 provinces of a fictional country (fictional sample data; the province names and numbers are not real).
At first glance at any dataset you want to know three things: how many rows there are, what types the columns were read as, and what the unit of observation is, that is, what each row describes. In this table a row is not a person; knowing this will help you later with the question "what does this data say about people?"
Then sort by population and look at the three most populous and the three least populous provinces: the gap between the ends gives you a first clue about the distribution.
This step's code cell is for members.Sign up free
The first way to see the distribution of a numeric variable is a histogram (lesson 11). Draw a histogram of population: try bins=10 first, then raise it to 30 and look at what changes. The check expects the final version with 30 bins.
Put the unit in the axis label (like "Population (people)"), say on the vertical axis what is being counted, and let the title say what the chart shows.
Then split population into the five bins provided and make a frequency table that counts how many provinces fall in each bin. The bin edges double at every step: if we had chosen equal-width bins, almost all provinces would fall in the first bin.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Look at the histogram and ask yourself the three questions from lesson 11: where is the peak, which way does the tail go, and is there a lone bar far away?
This step's code cell is for members.Sign up free
The editor says "the average province." First work out the two measures of center: the mean and the median population (lesson 12). Then answer this question with a number: what share of the provinces have a population below the mean? If the mean really described a "typical" province, this share would be close to half.
Check the shape with a number too: skew() gives the skewness coefficient (lesson 16). For comparison, print the same coefficient for median_age.
Finally, make your decision: which measure will you tell the editor for the "typical province"? Write your answer in the variable report_measure as "mean" or "median".
This step's code cell is for members.Sign up free
Center alone is not enough; you must also say how scattered the provinces are. Work out two measures of spread for population: the IQR (lesson 15) and the standard deviation (lesson 14). Then leave Grandton out and work them out again. Which measure does a single province move more?
The second question is about groups: how does median age change from region to region? Draw median_age as side-by-side box plots by region, and collect each region's number of provinces, median and IQR in one table. Sort the table by IQR from largest to smallest.
Looking at the table, ask yourself: is the region with the tallest box the region with the most provinces? What does the length of a box tell you, and what does it not tell you?
This step's code cell is for members.Sign up free
The editor also asks, "What is the average household size in the country?" A friend has worked out the averages region by region and suggests taking the average of those. Here is the trap from lesson 13: the simple average of group averages is the overall average only if the groups have equal weights.
First put the number of provinces and the average household size for each region in a table. Then work out (a) the simple average of the region averages and (b) their average weighted by the number of provinces. You will see that (b) matches the simple average of all provinces.
But the editor's question is about the country, not the provinces: should Grandton, with 4.9 million people, count the same as a province of 100 thousand? Decide which weight is right for the whole country ("provinces" or "population") and work out the population-weighted average. (The exact answer weights each province by its number of households; the table has no household counts, and population is the closest weight.)
This step's code cell is for members.Sign up free
Grandton stands alone at the far right of the histogram. How far out is it? The z-score (lesson 16) tells you how many standard deviations a value is from the mean.
Add z-score columns for two variables: z_pop and z_age. Then put Grandton and a province of your choice side by side. At the start, chosen is Redmoor; you can write the name of any other province from the table, and the check works with the province you choose.
Finally, print which of the two provinces is more unusual on each measure (the one whose z-score has the larger absolute value).
This step's code cell is for members.Sign up free
The editor will put a single chart in the story. The question is a comparison: how much more populous are the most populous provinces? According to lesson 17, the way to compare a number across categories is a bar chart; use horizontal bars so that the province names are easy to read one below another.
Take the 10 most populous provinces and sort them from smallest to largest: barh puts the first bar at the bottom, so the most populous province shows at the top. In a bar chart the axis starts at zero. The title should say what the chart finds, not just be a label like "Population"; if you can, include a number.
Going further (optional, not checked): draw the log-scale histogram from lesson 16 for population. Open a new figure (fig2, ax2 = plt.subplots()), pick the bin edges with np.geomspace(80_000, 5_000_000, 16) and write ax2.set_xscale("log"). What does the right-piled distribution look like on this scale?
This step's code cell is for members.Sign up free
Describe what you found to the editor in six sentences: (1) center, (2) spread, (3) shape, (4) unusual observation, (5) group comparison, (6) something this data does not say. Write the text in the description field of the cell; this part is not checked automatically, so assess it yourself with the list below.
Checklist
A weak example: "The average population of a province is 357,509 people. The population distribution is normal. Grandton is an error." It has three mistakes: the wrong measure of center, the wrong shape, and treating an unusual observation as an error.
The checked part: the cell prints eight sentences. The data supports four of them and does not support four. Write the letters of the ones it supports in the list says, and the ones it does not support in the list does_not_say.
Quick review (no points)
The editor wants to write "the average province has 357 thousand people." Why does this sentence mislead the reader?
In the median age box plot, the tallest box belongs to the Coast region. What does this say?
The average household size came out 3.222 weighted by the number of provinces and 3.162 weighted by population. Where does the difference come from?
Suppose you did the same task with the annual revenue of 81 companies in an industry instead of the population of provinces. Which steps would stay the same?
This step's code cell is for members.Sign up free