Source: U9L3 Activity Guide, Code.org App Lab
Tags: filtering data, histograms, datasets, data analysis, bucket size, female state legislators, CSP, AP Computer Science Principles
Difficulty: Beginner | Prerequisites: Comfort with datasets, tables, and basic chart reading (Unit 9 Lessons 1–2).
Unit 9 is about using data to discover patterns and draw conclusions. Filtering is one of the core operations you will use on any dataset: it lets you narrow a large table down to just the rows that match a condition you care about, so the chart or summary you build afterwards reflects a specific slice of the data rather than the whole thing. This lesson uses two real-world datasets, Female State Legislators and US Women Running for Elected Office in 2020, to practise filtering and then visualising the filtered results as histograms and bar charts in Code.org App Lab. You should already be comfortable loading a dataset and reading a simple chart before starting here.
Filtering a dataset means keeping only the rows that satisfy a condition (for example, Year = 2019) and hiding the rest. Once filtered, you can build a histogram or bar chart from the subset to spot patterns you would miss in the full dataset. Comparing filtered views across different years or categories is how you turn raw numbers into observations.
Filtering
Selecting a subset of rows from a dataset based on a specified condition. Only rows where the condition is true are kept; everything else is temporarily hidden.
In simple terms: you tell the tool "show me only the rows where Year equals 2019" and it hides every other year.
Histogram
A chart that groups numerical data into consecutive, equal-width ranges (called buckets or bins) and shows how many values fall into each range as bars. The bars touch because the ranges are continuous.
Think of it as sorting test scores into bins like 0–10, 10–20, 20–30 and then counting how many scores land in each bin.
Bucket size (bin width)
The width of each range on the x-axis of a histogram. A bucket size of 5 means each bar covers a 5-unit span (0–5, 5–10, 10–15, and so on). Choosing a different bucket size changes the shape of the histogram.
In simple terms: a bigger bucket lumps more values together, giving fewer, wider bars; a smaller bucket splits values into more, narrower bars.
Dataset
A structured collection of data organised in rows and columns, like a spreadsheet. Each row is one record (for example, one state in one year) and each column is one attribute (for example, Percentage of Females in Legislature).
Bar chart
A chart that uses separated bars to compare values across distinct categories. Unlike a histogram, the categories are not continuous ranges.
Think of it as one bar per category, with gaps between them, showing a count or total for each.
Open the Data Table Viewer in Code.org App Lab.
Choose the column you want to filter by (for example, "Year").
Set a condition (for example, Year = 2008) so only rows matching that year remain visible.
The filtered table is what you then visualise. The underlying data is unchanged; the filter just controls which rows are shown.
After filtering, select the numerical column you want to chart (for example, "Percentage of Females in Legislature").
Choose "Histogram" as the chart type.
Set your bucket size. In this activity, a bucket size of 5 is used, meaning each bar covers a 5-percentage-point range.
The y-axis ("Count") shows how many states fall into each bucket. The x-axis shows the percentage ranges.
Chart 1 in the activity filters the Female State Legislators dataset to a single birth year (the example uses 2008). The histogram shows most states clustered in the 15–30% range for female legislators.
Chart 2 filters the same dataset to 2019. The distribution shifts: more states appear in the higher-percentage buckets, and the spread is wider (roughly 20–60 total female legislators per state).
Comparing the two charts reveals a factual increase in the number of states with higher female representation over that period.
The activity distinguishes between stating what the data shows (factual) and explaining why it might show that (assumption).
A factual observation: "More states had 25–30% female legislators in 2019 than in 2008."
An assumption: "Laws modernising women's participation in government may explain this increase."
The exam and this course expect you to know the difference and to label each correctly.
This dataset records women who ran for elected office across all 50 states in 2020, broken down by office type (Governor, Senate, House, and so on).
The activity asks you to filter by office type and then count or chart the results.
Example question: "Which states had at least one woman run for Governor?" The answer drawn from the filtered data is 8 states.
The chart type is your choice: bar chart, histogram, or another visualisation that answers the question clearly.
Filtering is exactly what analysts do when they query a database: "Show me all sales from Q3" or "List patients over 65." Every spreadsheet, SQL query, and data dashboard relies on this same operation. Histograms with different bucket sizes are used in fields from quality control (distribution of part dimensions in manufacturing) to social science (income distributions across populations).
Students often confuse histograms with bar charts. A histogram groups continuous numerical data into buckets; a bar chart compares discrete categories. Histogram bars touch; bar chart bars have gaps.
Students sometimes think filtering changes the original dataset. It does not. Filtering only controls which rows are displayed or included in a calculation; the underlying table stays intact.
Changing the bucket size does not change the data, but it does change the shape of the histogram. A larger bucket can hide variation; a smaller bucket can make the chart look noisy. Students sometimes pick a bucket size without considering how it affects readability.
Students often jump straight from a chart to a causal explanation ("women are more represented because of new laws"). The data only shows what happened, not why. Separating observation from assumption is a key skill this lesson tests.
⚠️ The AP CSP exam expects you to interpret a visualisation of filtered data and state what it shows factually, without inferring causation.
⚠️ Know the difference between a histogram and a bar chart. Expect a question that asks you to pick the right chart type for a given dataset.
⚠️ Be able to read a histogram's x-axis bucket labels and y-axis count to answer "how many items fall in range X to Y?"
⚠️ Understand that the same dataset filtered to different values can produce very different-looking charts, and be prepared to compare two such charts.
True or false: A histogram's bars should have gaps between them. (False: histogram bars touch because the ranges are continuous.)
Fill in the blank: Filtering a dataset by Year = 2019 means only rows where the Year column equals ______ are shown. (2019)
True or false: Changing the bucket size of a histogram changes the underlying data. (False: it only changes how the data is grouped visually.)
Fill in the blank: Stating "more states had higher female representation in 2019 than in 2008" is a factual ______, not an assumption. (observation)
True or false: A bar chart is the same thing as a histogram. (False: bar charts compare discrete categories; histograms group continuous numerical data into buckets.)
Q: A histogram of "Percentage of Females in Legislature" filtered to 2008 uses a bucket size of 5. The bar covering 25–30% has a height of 7. What does that tell you?
A: Seven states had between 25% and 30% of their legislature made up of female legislators in 2008.
Q: The same histogram filtered to 2019 shows the 25–30% bar at a height of 9. What factual statement can you make by comparing the two charts?
A: More states (9 vs. 7) fell into the 25–30% female-legislator range in 2019 than in 2008.
Q: A classmate says, "The 2019 chart proves that new laws caused more women to enter legislatures." Is this a factual observation or an assumption? Explain.
A: It is an assumption. The chart shows that more states had higher percentages of female legislators in 2019, but it does not show the cause. Attributing the change to new laws goes beyond what the data alone can demonstrate.
Q: You filter the US Women Running for Elected Office in 2020 dataset by Office = Governor. The resulting table has rows for 8 different states. What does this tell you?
A: Eight states had at least one woman run for Governor in 2020.
Q: Why might choosing a bucket size of 1 instead of 5 make a histogram harder to read?
A: A bucket size of 1 produces many narrow bars, most with very small counts, making patterns difficult to spot. A larger bucket groups values together so broader trends become visible.
This connects to data collection and cleaning (Unit 9 Lessons 1–2) because filtering is useless if the dataset itself is incomplete or poorly structured. It also connects to drawing conclusions from data (later in Unit 9), where you move from observation to supported claims, the exact boundary this lesson practises. More broadly, filtering and visualisation are foundational to any data science or statistics course you take later.
filtering data, data filter, histogram, bar chart, bucket size, bin width, dataset, data visualisation, data visualization, Code.org App Lab, Female State Legislators, US Women Running for Elected Office 2020, AP CSP, AP Computer Science Principles, Unit 9 Lesson 3, U9L3, observation vs assumption, factual observation, data interpretation, chart reading, frequency distribution