<jupyter_start><jupyter_text>Comparing Baltimore and New York City Teenage Birth Rates by Race* How does teenage birth rates differ within the two cities by income level when stratifying for all income, high income, middle income, and low income?* Do we see similar trends for teenage birth rates between the two cities?<jupyter_code># pip install plotly
# for data analysis
import pandas as pd
import numpy as np

# for viz
import plotly.express as px
# import data

# baltimore teenage birth rates by income levels
df_balti_teenagebirths = pd.read_csv("https://raw.githubusercontent.com/katiesunsg/comparing-baltimore-nyc-teenagebirthrate/master/Balti-Raw-TeenageBirthRate-Income_AllHighMiddleLow.csv")

# nyc teenage birth rates by income levels 
df_nyc_teenagebirths = pd.read_csv("https://raw.githubusercontent.com/katiesunsg/comparing-baltimore-nyc-teenagebirthrate/master/NYC-Raw-TeenageBirthRate-Income_AllHighMiddleLow.csv")

# preview balti data
df_balti_teenagebirths.head()
# preview nyc data
df_nyc_teenagebirths.head()
# preview nyc data end
df_nyc_teenagebirths.tail()<jupyter_output><empty_output><jupyter_text>Filtering Data for Neighborhoods<jupyter_code># look at balti neighborhood names
df_balti_teenagebirths["Neighborhood"].unique()
# number of balti neighborhoods
df_balti_teenagebirths["Neighborhood"].nunique()
# nyc neighborhood names
df_nyc_teenagebirths["Neighborhood"].unique()
# number of nyc neighborhoods
df_nyc_teenagebirths["Neighborhood"].nunique()
# look at all of the column names
df_balti_teenagebirths.columns
df_balti_teenagebirths.info()
df_balti_teenagebirths.describe()
# rename columns teenage birth rates for balti
df_balti_teenagebirths = df_balti_teenagebirths.rename(columns={"Teenage_Birth_Rate_women_only_rP_gF_pall":"Teenage Birth Rate"})

# preview new column names
df_balti_teenagebirths.head()
# rename columns teenage births for nyc
df_nyc_teenagebirths = df_nyc_teenagebirths.rename(columns={"Teenage_Birth_Rate_women_only_rP_gF_pall":"Teenage Birth Rate"})

# preview new column names
df_nyc_teenagebirths.head()<jupyter_output><empty_output><jupyter_text>Removing Null Values for Teenage Birth Rates<jupyter_code># check for number of null values in balti df
df_balti_teenagebirths.isnull().sum()
# check for number of null values in nyc df
df_nyc_teenagebirths.isnull().sum()
# remove null values for balti 
modified_df_balti_teenagebirths = df_balti_teenagebirths.dropna()
# check balti null values were removed
modified_df_balti_teenagebirths.isnull().sum()
# remove null values for nyc
modified_df_nyc_teenagebirths = df_nyc_teenagebirths.dropna()
# check nyc null values were removed
modified_df_nyc_teenagebirths.isnull().sum()<jupyter_output><empty_output><jupyter_text>Pivot Tables with Teenage Birth Rates per Neighborhood by Income Level<jupyter_code># pivot table for balti teenage birth rates by income level 
pvmodified_df_balti_teenagebirths = pd.pivot_table(modified_df_balti_teenagebirths,
                    index = "Neighborhood",
                    columns = "Income",
                    values = "Teenage Birth Rate",
                    aggfunc = np.mean).reset_index()
# preview balti pivot table
pvmodified_df_balti_teenagebirths.head(15)
# pivot table for nyc teenage birth rates by income level
pvmodified_df_nyc_teenagebirths = pd.pivot_table(modified_df_nyc_teenagebirths,
                    index = "Neighborhood",
                    columns = "Income",
                    values = "Teenage Birth Rate",
                    aggfunc = np.mean).reset_index()
# preview nyc pivot table
pvmodified_df_nyc_teenagebirths.head(15)
# sort balti by all income
pvmodified_df_balti_teenagebirths = pvmodified_df_balti_teenagebirths.sort_values("All")
# preview data
pvmodified_df_balti_teenagebirths.head(10)
# sort nyc by all income
pvmodified_df_nyc_teenagebirths = pvmodified_df_nyc_teenagebirths.sort_values("All")

# preview data
pvmodified_df_nyc_teenagebirths.head(10)<jupyter_output><empty_output><jupyter_text>Create Baltimore Data Visualization<jupyter_code># bar graph for teenage birth rates in baltimore all income 
bar_baltiallteenagebirthrate = px.bar(pvmodified_df_balti_teenagebirths,x= "Neighborhood", y = "All", title = "Teenage Birth Rates in Baltimore City by All Income")
# data visualization 
bar_baltiallteenagebirthrate
# bar graph for teenage birth rates in nyc all income 
bar_nycallteenagebirthrate = px.bar(pvmodified_df_nyc_teenagebirths,x= "Neighborhood", y = "All", title = "Teenage Birth Rates in New York City by All Income")
# data visualization 
bar_nycallteenagebirthrate
# bar graph for teenage birth rates in baltimore low income 
bar_baltilowteenagebirthrate = px.bar(pvmodified_df_balti_teenagebirths,x= "Neighborhood", y = "Low", title = "Teenage Birth Rates in Baltimore City by Low Income")
# data visualization 
bar_baltilowteenagebirthrate
# bar graph for teenage birth rates in nyc low income 
bar_nyclowteenagebirthrate = px.bar(pvmodified_df_nyc_teenagebirths,x= "Neighborhood", y = "Low", title = "Teenage Birth Rates in New York City by Low Income")
# data visualization 
bar_nyclowteenagebirthrate<jupyter_output><empty_output><jupyter_text>Pivot Tables by City<jupyter_code># pivot table for overall nyc teenage birth rates by income level
citypvmodified_df_nyc_teenagebirths = pd.pivot_table(modified_df_nyc_teenagebirths,
                    index = "City",
                    columns = "Income",
                    values = "Teenage Birth Rate",
                    aggfunc = np.mean).reset_index()
# nyc overall table
citypvmodified_df_nyc_teenagebirths.head()
# pivot table for overall balti teenage birth rates by income level
citypvmodified_df_balti_teenagebirths = pd.pivot_table(modified_df_balti_teenagebirths,
                    index = "City",
                    columns = "Income",
                    values = "Teenage Birth Rate",
                    aggfunc = np.mean).reset_index()
# balti overall table
citypvmodified_df_balti_teenagebirths.head()<jupyter_output><empty_output><jupyter_text>Merge Condensed Datasets to Create Bar Graph<jupyter_code>#using base function merge
df_combinedcities = pd.concat([citypvmodified_df_balti_teenagebirths,citypvmodified_df_nyc_teenagebirths],axis=0)
# viewing combined dataset for overall cities
df_combinedcities.head()
# bar graph for overall teenage birth rates in balti and nyc all income
bar_combinedcities = px.bar(df_combinedcities,x= "City", y = ("All"), title = "Teenage Birth Rates in Baltimore City vs. New York City for All Income")
# view bar graph for two cities
bar_combinedcities
# bar graph for overall teenage birth rates in balti and nyc low income
bar_lowcombinedcities = px.bar(df_combinedcities,x= "City", y = ("Low"), title = "Teenage Birth Rates in Baltimore City vs. New York City for Low Income")
# view bar graph for two cities
bar_lowcombinedcities<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07214254671881791, 'fraction_numerical': 0.001303780964797914, 'mean_word_length': 5.339761248852158, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '633570', 'license': 'MIT', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2238, 'n_words': 574, 'path': 'baltimore_nyc_teenagebirthrate_income_katiesun.ipynb', 'repo_name': 'katiesunsg/comparing-baltimore-nyc-teenagebirthrate-income', 'size': '6903'}