
On Monday you and a partner worked out what was in the National Park Service visitor records and wrote five factual claims about them. You never got to ask the obvious question, because you did not yet have the tools.
Today you do. Every question below is a βwhich oneβ or a βtop tenβ question, and every one of them is answered with the two sentences you learned this morning.
Work in pairs, in one shared notebook, taking turns at the keyboard. Swap every time you finish a numbered task. The person not typing says out loud what they expect the answer to look like before the cell is run.
You have 45 minutes.
The data
The same file you explored on Monday:
https://eds-217-essential-python.github.io/data/national_parks.csv
Two things you found on Monday that will matter today:
- the
yearcolumn is stored as text, not as numbers - some rows have a
yearofTotalrather than an actual year
Setup
Create a notebook named Colab_3D_Ranking_Questions.ipynb, with both partnersβ names in the title cell, then read the file in and check its shape.
The two sentences
You have these on your session pages. You should not need to look them up more than once or twice today.
df[df['column'] > value] # the filter sentence
df.sort_values('column', ascending=False).head(n) # the top-N sentencePart 1: Filtering (about 15 minutes)
Answer each question with code, then write the answer in a markdown cell underneath, in a complete sentence with the number in it.
How many rows describe units whose
unit_typeis exactlyNational Park?How many rows have a
yearofTotal? Use~or!=to build a table calledby_yearthat has none of them in it. How many rows doesby_yearhave?The
regioncolumn uses two-letter codes. Build a list of the codesPW,IM, andAK, and use.isin()to filterby_yeardown to units in those three regions. How many rows?Using two conditions in one filter, find the rows that are National Parks and recorded more than 5 million visitors. How many are there, and what does each row represent?
Every comparison inside a combined filter needs its own set: (a == b) & (c > d). If you get a TypeError that mentions something you did not write, missing parentheses are almost always the reason.
Part 2: Ranking (about 15 minutes)
Rank all of
parksbyvisitors, largest first, and look at the top 10. Something is wrong with this answer. Say what, and say why in one sentence.Fix it, and rank
by_yearinstead. Which single park unit had the highest visitor count in any one year, and in what year?Filter to National Parks in the year
2016, then find the top 10 by visitors. Show only theunit_name,state, andvisitorscolumns.How many of the 61 National Parks recorded more than a million visitors in 2016?
Use
.idxmax()and.loc[]onnp_2016to print the name of the most-visited National Park of 2016 on its own, without displaying the whole row.
Part 3: Put them together (about 10 minutes)
Build a list of the state codes
CA,UT, andAZ. Filternp_2016to National Parks in those three states, rank them by visitors, and display the whole ranked list.Which of those three states has the park at the top of your list, and which has the most parks in it? Answer both in a markdown cell. You can count the parks per state by eye from your ranked table.
In one markdown cell of three or four sentences, answer this: a colleague asks you which national park is βthe busiestβ. What do you need to know before you can answer, and what would you tell them? Use at least two numbers from todayβs work.
Pick a year other than 2016 and produce the same top 10. Did the order change? Name one park that moved several places, and say what you would need in order to find out whether that movement is real or an artifact of how the data was recorded.
Do not try to compare all the years at once. That needs grouping, and grouping is Friday.
Wrap-up
Weβll hear two or three pairs on question 12. Be ready to say which filter you would insist on before answering, not just what your answer was.