
Yesterday you and a partner worked out what was in the National Park Service visitor records and wrote five factual claims about those records. Now we will use our new tools and methods to dig deeper into the data. Specifically, we will now be able to ask βwhich oneβ and βtop tenβ questions. Every one of the code questions below uses these new patterns.
Work in pairs, in one shared notebook, taking turns at the keyboard. Swap every time you finish a numbered task. The person not typing should read the code through (quietly) and say what they expect it to produce before you run it.
We have about 45 minutes for this activity: roughly 40 on the three parts below, and the rest on setting up and on the wrap-up. Please tell Cella or Kelly if you get stuck, rather than spending ten of those minutes on one line!
The data
The same file you explored yesterday:
https://eds-217-essential-python.github.io/data/national_parks.csv
Two things you saw yesterday 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.
Our two new patterns
You have both of these in your session notebooks already, the filter pattern from this morning and the top-N pattern from earlier this afternoon, but here they are as a reminder:
df[df['column'] > value] # the filter pattern
df.sort_values('column', ascending=False).head(n) # the top-N patternPart 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 on
by_year, find the rows that are National Parks and recorded more than 5 million visitors. How many are there, and what does each row represent? Useby_yearrather thanparks, because theTotalrows you removed in question 2 would otherwise be counted alongside the single years.
Every comparison inside a combined filter needs its own set of parentheses: (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.Your
np_2016table has one row per National Park that reported a 2016 figure. Print its shape to see how many parks that is, then find how many of them recorded more than a million visitors.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, if any of them did, 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 we get to grouping later this week.
Wrap-up
If there is time, we will hear from two or three pairs regarding their answers to question 12. Be ready to say which filter you would insist on before answering, not just what your answer was.