# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# %matplotlib inline

police_killings = pd.read_csv("police_killings.csv", encoding="iso-8859-1")

police_killings.head()

print(police_killings.columns)

police_killings["raceethnicity"].value_counts()

police_killings["raceethnicity"].value_counts().plot(kind="bar")
plt.show()

police_killings["raceethnicity"].value_counts() / sum(police_killings["raceethnicity"].value_counts())

# ### Police killings by race
# About 50% of the police killings sample are for white people, but about 77% of the population in the US as a whole is white. At the same time, almost 29% of people killed by police in this dataset were black, whereas only 13% of the US population as a whole is black. Clearly, blacks are over-represented in the police killings dataset, and whites are under-represented.

income = police_killings[police_killings["p_income"] != "-"]["p_income"].astype(int)

income.hist(bins=50, figsize=(15,6))

state_pop = pd.read_csv("state_population.csv")

counts = police_killings["state_fp"].value_counts()

states = pd.DataFrame({
        "STATE": counts.index,
        "shootings": counts
    })

states.head()

states = pd.merge(state_pop, states, on="STATE")

states["pop_millions"] = states["POPESTIMATE2015"] / 1000000

states["pop_millions"].head()

states["rate"] = states["shootings"] / states["pop_millions"]

states.sort("rate").head()

states.sort("rate", ascending=False).head()

bad_rows = police_killings[["share_white", "share_black", "share_hispanic"]].replace({"-": None}).isnull()

police_killings[bad_rows == True].shape

police_killings[police_killings["share_white"] == "-"]

null_share_white = police_killings["share_white"] == "-"
null_share_black = police_killings["share_black"] == "-"
null_share_hispanic = police_killings["share_hispanic"] == "-"

police_killings[null_share_white | null_share_black | null_share_hispanic]

police_killings[null_share_hispanic][["share_white", "share_black", "share_hispanic"]]

pk = police_killings[(police_killings["share_white"] != "-") & 
                     (police_killings["share_black"] != "-") & 
                     (police_killings["share_hispanic"] != "-")].copy()

pk.shape

police_killings.shape

pk["share_white"] = pk["share_white"].astype(float)
pk["share_black"] = pk["share_black"].astype(float)
pk["share_hispanic"] = pk["share_hispanic"].astype(float)

lowest_rates = states.sort("rate").head(10)
highest_rates = states.sort("rate", ascending=False).head(10)

lowest_rates

pk_lowest = pk[pk["state_fp"].isin(lowest_rates["STATE"])]
pk_highest = pk[pk["state_fp"].isin(highest_rates["STATE"])]

pk_lowest[["share_white", "share_black", "share_hispanic"]].mean()

pk_highest[["share_white", "share_black", "share_hispanic"]].mean()



StarcoderdataJupyterScriptsDedupFiltered