Skip to content

Voter Registration over time

Let's look at the percentage of the population that registers to vote over time.

  1. First we'll plot total registration percentages from 1966-2022.
  2. Then we'll separate voters into age groups and see what proportion of different age groups register to vote over time.
  3. Lastly, we'll break down voters into demographic groups and see what those percentages look like.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
voter_reg = pd.read_excel('1964-2022_registrationdata.xlsx')

Look at the data

voter_reg.info()
voter_reg.head()

Sort for the 'total' age group. Do it with python and with SQL

total_reg = voter_reg[voter_reg['age_group'] == 'total'].sort_values('year')
total_reg
Spinner
DataFrameas
total
variable
SELECT *
FROM voter_reg
WHERE age_group = 'total'
ORDER BY year;

Total Voter Registration

fig, ax = plt.subplots(figsize=(10,6))
sns.lineplot(data=total, x='year', y='total_reg')
plt.show()

Take the total age group out (with python and SQL) then plot another line graph with all age groups except the total.

Spinner
DataFrameas
age_groups
variable
SELECT *
FROM voter_reg
WHERE age_group <> 'total'
ORDER BY age_group, year;
age_groups_nototal = voter_reg[voter_reg['age_group'].isin(['18-24', '25_44', '45_64', '65_plus'])].sort_values(['age_group', 'year'])
age_groups_nototal

Voter Registration by Age

fig, ax = plt.subplots(figsize=(10,6))
sns.lineplot(data=age_groups, x='year', y='total_reg', hue='age_group')
ax.set_title('Voter Registration time Series by Age Group')
ax.spines[['left', 'top', 'right']].set_visible(False)
plt.show()

I realized that the 18-24 age group label was created wrong (18-24 instead of 18_24), so I'll try to fix that