This week we’re exploring U. S. judge data from the {historydata} R package! This dataset contains information about the appointments and careers of all federal judges in United States history since 1789.
TidyTuesday
Data Visualization
Python
Pandas
Polars
Numpy
2025
Author
Peter Gray
Published
June 15, 2025
1. Python code
Show code
# ! pip install polars# ! pip install pandas# ! pip install numpy# ! pip install folium# ! pip install geopyimport polars as plimport pandas as pdimport numpy as npimport plotly.express as pximport plotly.io as pioimport matplotlib.pyplot as pltpio.templates.default ='plotly_white'judges_appointments = pd.read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-06-10/judges_appointments.csv')judges_people = pd.read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-06-10/judges_people.csv')merged_df = pd.merge(judges_appointments, judges_people, on ='judge_id', how ='left')merged_df["commission_date"] = pd.to_datetime(merged_df["commission_date"], format="%m/%d/%Y")merged_df = merged_df.assign(Non_white = np.where(merged_df["race"] !="White", "Non-White", "White"))# Top 100 judges per apppointmenttop10 = pd.DataFrame(merged_df.groupby(['president_name']).president_name.value_counts().nlargest(10)).reset_index()top10.columns = ['president_name', 'count']pio.templates.default ='plotly_white'# NUmber of judges per partyparties = ["Democratic", "Republican"]party = merged_df[merged_df["president_party"].isin(parties)]parties = pd.DataFrame(party.groupby("president_party").size()).reset_index()parties.columns = ['president_party', 'count']fig, ax = plt.subplots(figsize=(10, 6));bars = ax.bar(x=top10['president_name'], height=top10['count'], color='skyblue')ax.set_xlabel('President Name')ax.set_ylabel('Number of Appointments per President')ax.set_title('Top 10 Presidents by Number of Appointments')ax.tick_params(axis='x', rotation=45)ax.spines['top'].set_visible(False)ax.spines['right'].set_visible(False)for bar in bars: height = bar.get_height() ax.annotate(f'{int(height)}', xy=(bar.get_x() + bar.get_width() /2, height), xytext=(0, 3), # Offset above bar textcoords='offset points', ha='center', va='bottom', fontsize=9, color='black' )plt.tight_layout()# By Politcial PArtyparty_colors = {"Democratic": "blue","Republican": "red"}colors = parties['president_party'].map(party_colors)# Create plotfig1, ax = plt.subplots(figsize=(10, 6));bars = ax.bar(x=parties['president_party'], height=parties['count'], color=colors)ax.spines['top'].set_visible(False)ax.spines['right'].set_visible(False)ax.set_xlabel('Political Party')ax.set_ylabel('Number of Appointments per Party')ax.set_title('Number of Appointments by Political Party')ax.tick_params(axis='x', rotation=0)# Add count labels on top of barsfor bar in bars: height = bar.get_height() ax.annotate(f'{int(height)}', xy=(bar.get_x() + bar.get_width() /2, height), xytext=(0, 3), textcoords='offset points', ha='center', va='bottom', fontsize=9, color='black' )plt.tight_layout()female_judge_percent = merged_df.groupby("gender").size()percent_df = female_judge_percent.div(female_judge_percent.sum()).mul(100)dict( value =f"{round(percent_df.get('F', 0), 1)}%")cut_off = pd.to_datetime("1969-01-01")pre_1968 = merged_df[merged_df["commission_date"] <= cut_off]non_white_number = pre_1968.groupby("Non_white").size()non_White_percent = non_white_number.div(non_white_number.sum()).mul(100)dict( value =f"{round(non_White_percent.get('Non-White', 0), 1)}%");repub = merged_df[merged_df["president_party"].isin(["Republican", "Democratic"])]repub_number = repub.groupby("president_party").size()repub_percent = repub_number.div(repub_number.sum()).mul(100)dict( value =f"{round(repub_percent.get('Republican', 0), 1)}%");