Creating a Frequency Bar Chart from a DataFrame
Bar charts are excellent visualizations to show the frequency of categorical data. Often, it is valuable to quickly create a frequency bar chart from a DataFrame of thousands of observations.
When visualizing complex data, such as a simulation of guessing on a true/false exam with 10 questions, we can quickly create a frequency bar chart of this data:
Creating a Frequency Bar Chart
To understand the code to create a frequency bar chart, let's start with a simple DataFrame that stores data about each pizza that was ordered today:
import pandas as pd
df = pd.DataFrame([
{"pizzaOrder": "Cheese", "cost": 14.99},
{"pizzaOrder": "Cheese", "cost": 14.99},
{"pizzaOrder": "Pepperoni", "cost": 16.99},
{"pizzaOrder": "Cheese", "cost": 14.99},
{"pizzaOrder": "Veggie", "cost": 16.99},
{"pizzaOrder": "Veggie", "cost": 16.99},
{"pizzaOrder": "Pepperoni", "cost": 16.99},
{"pizzaOrder": "Veggie", "cost": 16.99},
{"pizzaOrder": "Cheese", "cost": 14.99},
{"pizzaOrder": "Cheese", "cost": 14.99},
])
df
pizzaOrder | cost | |
---|---|---|
0 | Cheese | 14.99 |
1 | Cheese | 14.99 |
2 | Pepperoni | 16.99 |
3 | Cheese | 14.99 |
4 | Veggie | 16.99 |
5 | Veggie | 16.99 |
6 | Pepperoni | 16.99 |
7 | Veggie | 16.99 |
8 | Cheese | 14.99 |
9 | Cheese | 14.99 |
Using this DataFrame, the bar chart graphs the price of each pizza in the list and isn't extremely useful:
Finding the Counts of Unique Values
The panda's function value_counts()
counts the number of times each unique value appears in a column of a DataFrame. In our sample DataFrame, we can count the unique values of the pizzaOrder
:
Similarly, we can find the unique values for the cost
(value_counts()
works with numeric data in addition to strings!):
Creating a Bar Chart of Data Frequency (Counts)
When we use the value_counts()
functions, the result is a Series
and can be directly visualized -- it's common to see all of this done in a single line!
A frequency bar chart of the different types of pizza:
Similarly, a frequency bar chart of the different prices of pizza today:
Sorting the Bar Chart
When working with numeric values, sorting the order in which the values appear in the bar chart becomes critical. There are two ways we might need to sort the data:
- Sorting the data based on the category (ex: by the pizza toppings) so that the values along the x-axis are ordered, OR
- Sorting the data based on the frequency (the number of occurrences) so the categories are in increasing/decreasing frequency
We'll explore an simulation to see the two ways to sor the data.
Application: Large Scale Simulation of Guessing on an Exam
An extremely common application of frequency bar charts is in the exploration of simulation results. For example, the following code simulates taking an exam with 10 true/false questions and guessing on each question. The DataFrame stores the result of how many questions each of the 10,000 simulated students answers correctly when guessing randomly:
import pandas as pd
import random
data = []
for i in range(10000):
# Simulate taking a 10-question true/false exam, guessing on every question:
correctAnswers = 0
for q in range(10):
correctAnswers = random.choice([0, 1])
d = {"correctAnswers": correctAnswers}
data.append(d)
df = pd.DataFrame(data)
df
correctAnswers | |
---|---|
0 | 2 |
1 | 2 |
2 | 8 |
3 | 6 |
4 | 6 |
... | ... |
9995 | 7 |
9996 | 4 |
9997 | 4 |
9998 | 5 |
9999 | 5 |
By using value_counts()
and plot.bar()
, we can quickly visualize the distribution of the simulation:
Sorting By Frequency
By default, value_counts()
sorts the list of values by frequency so that the most common value appears first on the list returned by value_counts()
and will display on the left-hand side of the chart.
This creates a visualization with decreasing frequency moving from left-to-right, but with out-of-order data listed along the x-axis (note that the x-axis is 5, 4, 6, 7, 3 ...):
Sorting by Category/Index
It is common to want to sort by the category (stored in the index
of the Series returned by value_counts()
). To create a bar chart sorted by the index, we add sort_index()
after value_counts()
. The result is that the data is now sorted by the category name instead of the frequency:
Creating the bar chart:
We find our visualization now organizes the data by the category instead of the frequency: