Pandas Value Counts with Gubernatorial Data (Solution)

pandas
exercise
solution
Published

August 1, 2024

Load the data

Code
import pandas as pd
df  = pd.read_csv("https://raw.githubusercontent.com/melaniewalsh/responsible-datasets-in-context/refs/heads/main/datasets/gubernatorial-bios/gubernatorial_bios_final.csv")

Exercise 1

What are the top 5 birth states for governors?

Save as top_birth_states and then view the resulting dataframe.

Code
top_birth_states = (
    df['birth_state_territory']
    .value_counts()
    .head(5)
)
top_birth_states
birth_state_territory
New York          162
Virginia          147
Pennsylvania      112
Massachusetts     102
South Carolina    101
Name: count, dtype: int64

Discuss/consider: What states are the most common birth spots for governors? Are there similar traits between the top states?

Exercise 2

Let’s now look at the initial age for governors and the distribution. We already visualized the average starting age in the main data essay, but let’s look at the distribution across the entire dataframe

First, let’s cut the age_at_start variable into bins, so people between ages 20 and 30 get mapped to 20, 30 and 40 to 30 etc. Let’s call this new column age_bucket. We will use the Pandas function [pd.cut]{https://pandas.pydata.org/docs/reference/api/pandas.cut.html}.

Now let’s display the value_counts of the age_bucket column and let’s sort by the age.

Code
df['age_bucket'] = pd.cut(df['age_at_start'], bins=[20,30,40,50,60,70,80])
df['age_bucket'].value_counts().sort_index()
age_bucket
(20, 30]       7
(30, 40]     374
(40, 50]    1004
(50, 60]     789
(60, 70]     279
(70, 80]      32
Name: count, dtype: int64

Discuss/consider: Which age range has the most governors?

Exercise 3:

Let’s look at the outlier governors who started office at or below the age of 30. Let’s filter the dataset down to this set by filtering on the age_at_start column, call it young_governors and display it.

Code
young_governors = df[df['age_at_start'] <= 30]
young_governors
Unnamed: 0 state_territory governor party first_year years_in_office school birth_state_territory birth_date bio_text college_attendance ivy_attendance lawyer military_service age_at_start gender born_in_state_territory intl_born intl_born_details age_bucket
522 522 Georgia George Walton Whig, Democratic-Republican 1779 1779 - 1780 NaN Virginia 1749-01-01 GEORGE WALTON, the youngest signer of the Decl... 0 0 1 1 30 male 0 0 NaN (20, 30]
740 740 Kansas Samuel Johnson Crawford Republican 1865 1865 - 1868 Cincinnati College Indiana 1835-04-10 SAMUEL JOHNSON CRAWFORD, the third governor of... 1 0 1 1 30 male 0 0 NaN (20, 30]
833 833 Louisiana Henry Clay Warmoth Republican 1868 1868 - 1872 NaN Illinois 1842-05-09 HENRY C. WARMOTH was born in Mc Leansboro, Ill... 0 0 1 1 26 male 0 0 NaN (20, 30]
976 976 Maryland Edward Lloyd Democratic-Republican 1809 1809 - 1811 NaN Maryland 1779-07-22 EDWARD LLOYD was born in Talbot County, Maryla... 0 0 0 1 30 male 1 0 NaN (20, 30]
1453 1453 New Jersey Leon R. Taylor Democratic 1913 1913 - 1914 Denison University New Jersey 1883-10-26 Leon R. Taylor was born in Asbury Park, New Je... 1 0 0 0 30 male 1 0 NaN (20, 30]
1898 1898 Rhode Island William Sprague Republican 1860 1860 - 1863 NaN Rhode Island 1830-09-12 WILLIAM SPRAGUE was born in Cranston, Rhode Is... 0 0 0 1 30 male 1 0 NaN (20, 30]
2439 2439 Wyoming Amos Walker Barber Republican 1890 1890 - 1893 University of Pennsylvania Pennsylvania 1860-07-25 AMOS WALKER BARBER was born in Doylestown, Pen... 1 1 0 1 30 male 0 0 NaN (20, 30]

Discuss/consider: Who are the youngest governors in American History? What era are they from? Take a look at some of their Wikipedia pages and NGA biographies online.