<jupyter_start><jupyter_text>In this notebook we are going to take age group data from a California (by county) demographics dataset and compare them with Covid case counts per month of those same age groups.  Were any age groups impacted disproportionately given their percentage of cases and percentage of the population?<jupyter_code># import the various modules needed
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import pathlib
import numpy as np<jupyter_output><empty_output><jupyter_text>DEMOGRAPHICS DATA: Pull in the dataset.  Get columns required for analysis.  Calculate additional quantity columns needed for each age group.  Get population counts for each age group, for population total.  Get percentages of each age group of total population.<jupyter_code># Read demographics data into dataframe and drop last line containing NaN
demographfile = "../../Resources/Clean/ca_county_demographic_data-20210804.csv"
demograph_df = pd.read_csv(demographfile).dropna(subset=["FIPS Code"])
demograph_df.tail()
# Fix the data types in demographics dataframe
demograph_df["Total Population"] = demograph_df["Total Population"].str.replace(',', '').astype(float)
# Get age related subset of columns needed for analysis
demo_age_df = demograph_df[["County","Total Population","Pop % 65 and Over", "Pop % Less Than 18 Years of Age"]].copy()
demo_age_df.head()
# Extrapolate population quantity totals in each county for age groups, save in age groups df
pop_total_count  = demo_age_df["Total Population"]
pop_pct_under_18 = demo_age_df["Pop % Less Than 18 Years of Age"]
pop_pct_over_65  = demo_age_df["Pop % 65 and Over"]
demo_age_df["Pop # 0 to 17"]  = pop_total_count * pop_pct_under_18 / 100
demo_age_df["Pop # 18 to 64"] = pop_total_count * (100 - pop_pct_under_18 - pop_pct_over_65) / 100
demo_age_df["Pop # 65 to up"] = pop_total_count * pop_pct_over_65 / 100
demo_age_df.head()
# Get sum population per age group and total population for California from demographics
demo_age_summary = demo_age_df[["Total Population", "Pop # 0 to 17", "Pop # 18 to 64", "Pop # 65 to up"]].round().sum()
demo_age_summary
# Calculate percent of total California population that each age group represents
## Give shorter friendlier names for specific columns
pop_CA_0_17 = demo_age_summary["Pop # 0 to 17"]
pop_CA_18_64 = demo_age_summary["Pop # 18 to 64"]
pop_CA_65_up = demo_age_summary["Pop # 65 to up"]
pop_CA_total = demo_age_summary["Total Population"]

## Calculate and print population percentages for each age group for California
pop_pct_CA_0_17  = pop_CA_0_17  * 100 / pop_CA_total
pop_pct_CA_18_64 = pop_CA_18_64 * 100 / pop_CA_total
pop_pct_CA_65_up = pop_CA_65_up * 100 / pop_CA_total
pop_pct_CA_0_17, pop_pct_CA_18_64, pop_pct_CA_65_up<jupyter_output><empty_output><jupyter_text>COVID CASES DATA: Pull in the cleaned dataset.  Contains cases by row of each individual case reported in California by date, sex, age group, county, etc. Get columns required for analysis. Give each age group its own dataframe of data.<jupyter_code># Read in the monthly individual cases data from California into dataframe
cases_file = "../../Resources/Clean/ca_data_df.csv"
cases_df = pd.read_csv(cases_file)
cases_df["age_group"].value_counts()
# Create a subset of columns from the cases for our age_group analysis
cases_agegroup_df = cases_df[["case_month", "age_group"]].copy()
cases_agegroup_df.head()
# Group the cases by case_month then count the cases.  One df per age group, one df for totals
## Define Filters
age_group_0_17  = (cases_agegroup_df["age_group"] == "0 - 17 years")
age_group_18_49 = (cases_agegroup_df["age_group"] == "18 to 49 years")
age_group_50_64 = (cases_agegroup_df["age_group"] == "50 to 64 years")
age_group_18_64 = age_group_18_49 | age_group_50_64
age_group_65_up = (cases_agegroup_df["age_group"] == "65+ years")
age_group_all   = age_group_0_17 | age_group_18_49 | age_group_50_64 | age_group_65_up

## Group by case_month and then count cases.  Include one for grand total
cases_0_17_df   = (cases_agegroup_df
                   .loc[age_group_0_17]
#                    .rename(columns={"age_group": "0 to 17"})
                   .groupby(by="case_month")
                   .count()
                   .copy()
                  )
cases_18_49_df   = (cases_agegroup_df
                   .loc[age_group_18_49]
#                    .rename(columns={"age_group": "18 to 49"})
                   .groupby(by="case_month")
                   .count()
                   .copy()
                  )
cases_50_64_df   = (cases_agegroup_df
                   .loc[age_group_50_64]
#                    .rename(columns={"age_group": "50 to 64"})
                   .groupby(by="case_month")
                   .count()
                   .copy()
                  )
cases_18_64_df   = (cases_agegroup_df
                   .loc[age_group_18_64]
#                    .rename(columns={"age_group": "18 to 64"})
                   .groupby(by="case_month")
                   .count()
                   .copy()
                  )
cases_65_up_df   = (cases_agegroup_df
                   .loc[age_group_65_up]
#                    .rename(columns={"age_group": "65 and up"})
                   .groupby(by="case_month")
                   .count()
                   .copy()
                  )
cases_total_df   = (cases_agegroup_df
                    .loc[age_group_all]
#                    .rename(columns={"age_group": "all ages"})
                   .groupby(by="case_month")
                   .count()
                   .copy()
                  )
# Calculate percent of total cases per month that each age group comprised
pct_monthly_0_17  = (cases_0_17_df * 100 / cases_total_df).copy()
pct_monthly_18_49 = (cases_18_49_df * 100 / cases_total_df).copy()
pct_monthly_18_64 = (cases_18_64_df * 100 / cases_total_df).copy()
pct_monthly_50_64 = (cases_50_64_df * 100 / cases_total_df).copy()
pct_monthly_65_up = (cases_65_up_df * 100 / cases_total_df).copy()
pct_monthly_50_64.head()
# Rename the "age_group" column for the affected dataframes after doing the pct math
cases_0_17_df     = cases_0_17_df.rename(columns={"age_group": "0 to 17"})
cases_18_49_df    = cases_18_49_df.rename(columns={"age_group": "18 to 49"})
cases_18_64_df    = cases_18_64_df.rename(columns={"age_group": "18 to 64"})
cases_50_64_df    = cases_50_64_df.rename(columns={"age_group": "50 to 64"})
cases_65_up_df    = cases_65_up_df.rename(columns={"age_group": "65 and up"})
cases_total_df    = cases_total_df.rename(columns={"age_group": "all ages"})
pct_monthly_0_17  = pct_monthly_0_17.rename(columns={"age_group": "0 to 17"})
pct_monthly_18_49 = pct_monthly_18_49.rename(columns={"age_group": "18 to 49"})
pct_monthly_18_64 = pct_monthly_18_64.rename(columns={"age_group": "18 to 64"})
pct_monthly_50_64 = pct_monthly_50_64.rename(columns={"age_group": "50 to 64"})
pct_monthly_65_up = pct_monthly_65_up.rename(columns={"age_group": "65 and up"})
# One frame for all case counts 3 groups
frames = [cases_18_64_df, cases_0_17_df, cases_65_up_df]
all_ages_cases_df = pd.concat(frames, axis=1).fillna(0).copy()
all_ages_cases_df.head()
# One frame for all case counts 4 groups
frames = [cases_18_49_df, cases_0_17_df, cases_50_64_df, cases_65_up_df]
all_ages_4_groups_df = pd.concat(frames, axis=1).fillna(0).copy()
all_ages_4_groups_df.head()
## One dataframe with monthly percentage of the 3 age groups in demographics. Case month as index
frames = [pct_monthly_18_64, pct_monthly_0_17, pct_monthly_65_up]
all_ages_pct_df = pd.concat(frames, axis=1).fillna(0).copy()
all_ages_pct_df.head()
## One dataframe with monthly percentage of the 4 age groups in cases frame. Case month as index
frames = [pct_monthly_18_49, pct_monthly_50_64, pct_monthly_0_17, pct_monthly_65_up]
all_ages_pct_4_df = pd.concat(frames, axis=1).fillna(0).copy()
all_ages_pct_4_df.head()
# Reorder the age groups sequentially so they plot sequentially in each graph
all_ages_cases_df = all_ages_cases_df[["0 to 17", "18 to 64", "65 and up"]]
all_ages_4_groups_df = all_ages_4_groups_df[["0 to 17", "18 to 49", "50 to 64", "65 and up"]]
all_ages_pct_df = all_ages_pct_df[["0 to 17", "18 to 64", "65 and up"]]
all_ages_pct_4_df = all_ages_pct_4_df[["0 to 17", "18 to 49", "50 to 64", "65 and up"]]<jupyter_output><empty_output><jupyter_text>PLOTS: Plot monthly counts of cases per age group, percentage of cases vs percentage of population, stacked counts of cases per age group<jupyter_code># Identify location of output images
output_images = "../../Images"
plt.figure(figsize=(9,6))
plt.plot(all_ages_cases_df.index, all_ages_cases_df["0 to 17"], color="blue")
plt.plot(all_ages_cases_df.index, all_ages_cases_df["18 to 64"], color="orange")
plt.plot(all_ages_cases_df.index, all_ages_cases_df["65 and up"], color="green")
plt.title("Age Groups Cases over time", color="purple", fontsize=20)
plt.xticks(rotation="vertical")
plt.xlabel("Month", fontsize=15)
plt.ylabel("Cases", fontsize=15)
plt.legend(["0 to 17", "18 to 64", "65 and up"],loc="best")
plt.grid(axis="y")
plt.savefig(f"{output_images}/age_groups_monthly_counts.png")
plt.show()
# Plot monthly percentage of total cases for 0-17
plt.figure(figsize=(8,6))
plt.bar(pct_monthly_0_17.index, pct_monthly_0_17["0 to 17"], color="blue")
plt.title(f"Age Group 0-17\nCovid Cases in CA", color="purple", fontsize=20)
plt.xticks(rotation="vertical")
plt.xlabel("Month", fontsize=15)
plt.ylabel("% of total cases", fontsize=15)
plt.ylim(0, 100)
plt.grid(axis="y")
plt.axhline(y = pop_pct_CA_0_17,  linestyle = ":", color="purple")
plt.annotate(f"Group is {round(pop_pct_CA_0_17,1)}% of population",(0,24),fontsize=15,color="purple")
plt.savefig(f"{output_images}/age_group_0_17_cases.png")
plt.show()
# Plot monthly percentage of total cases for 18-64
plt.figure(figsize=(8,6))
plt.bar(pct_monthly_18_64.index, pct_monthly_18_64["18 to 64"], color="orange")
plt.title(f"Age Group 18-64\nCovid Cases in CA", color="purple", fontsize=20)
plt.xticks(rotation="vertical")
plt.xlabel("Month", fontsize=15)
plt.ylabel("% of total cases", fontsize=15)
plt.ylim(0, 100)
plt.grid(axis="y")
plt.axhline(y = pop_pct_CA_18_64,  linestyle = ":", color="purple")
plt.annotate(f"Group is {round(pop_pct_CA_18_64,1)}% of population",(0,64),fontsize=15,color="purple")
plt.savefig(f"{output_images}/age_group_18_64_cases.png")
plt.show()
# Plot monthly percentage of total cases for 65 and up
plt.figure(figsize=(8,6))
plt.bar(pct_monthly_65_up.index, pct_monthly_65_up["65 and up"], color="green")
plt.title(f"Age Group 65 and up\nCovid Cases in CA", color="purple", fontsize=20)
plt.xticks(rotation="vertical")
plt.xlabel("Month", fontsize=15)
plt.ylabel("% of total cases", fontsize=15)
plt.ylim(0, 100)
plt.grid(axis="y")
plt.axhline(y = pop_pct_CA_65_up,  linestyle = ":", color="purple")
plt.annotate(f"Group is {round(pop_pct_CA_65_up,1)}% of population",(7,17),fontsize=15,color="purple")
plt.savefig(f"{output_images}/age_group_65_up_cases.png")
plt.show()
# Plot stacked bar chart of cases by age group monthly (cases data + demographic data)
all_ages_cases_df.plot.bar(stacked=True, figsize=(8,6))
plt.gcf().axes[0].yaxis.get_major_formatter().set_scientific(False)
plt.title("Covid Cases by Age Group in CA", fontsize=20, color="purple")
plt.xlabel("Month", fontsize=15)
plt.ylabel("Cases", fontsize=15)
plt.grid(axis="y")
plt.savefig(f"{output_images}/age_groups_total_stacked_bar.png")
plt.show()
# Plot stacked bar chart of cases by age group monthly (cases data only, 4 age groups)
all_ages_4_groups_df.plot.bar(stacked=True, figsize=(9,6))
plt.gcf().axes[0].yaxis.get_major_formatter().set_scientific(False)
plt.title("Covid Cases by Age Group in CA", fontsize=20, color="purple")
plt.xlabel("Month", fontsize=15)
plt.ylabel("Cases", fontsize=15)
plt.grid(axis="y")
plt.savefig(f"{output_images}/age_groups_total_4_stacked_bar.png")
plt.show()
# Unstacked bar chart showing percentage cases by 3 age groups per month
all_ages_pct_df.plot.bar(stacked=True, figsize=(12,6))
plt.gcf().axes[0].yaxis.get_major_formatter().set_scientific(False)
plt.title("Covid % Cases by Age Group", fontsize=20, color="purple")
plt.xlabel("Month", fontsize=15)
plt.ylim(0,100)
plt.ylabel("% of Cases", fontsize=15)
plt.grid(axis="y")
plt.savefig(f"{output_images}/age_groups_pct_3_stacked_bar.png")
plt.show()
# Unstacked bar chart showing percentage cases by 4 age groups per month
all_ages_pct_4_df.plot.bar(stacked=True, figsize=(12,6))
plt.title("Covid % Cases by Age Group", fontsize=20, color="purple")
plt.xlabel("Month", fontsize=15)
plt.ylim(0,100)
plt.ylabel("% of Cases", fontsize=15)
plt.grid(axis="y")
plt.savefig(f"{output_images}/age_groups_pct_4_stacked_bar.png")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12465699725597805, 'fraction_numerical': 0.053469227753822034, 'mean_word_length': 4.743358847366052, 'pattern_counts': {'":': 20, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '624354', 'license': 'Apache-2.0', 'n_tokens_mistral': 5097, 'n_tokens_neox': 4535, 'n_words': 1073, 'path': 'Notebooks/Analysis_And_Visualizations/covid_infections_by_age_group.ipynb', 'repo_name': 'githotirado/covid-analysis', 'size': '12755'}