text_plain_output_1.png image_output_1.png
from collections import Counter

import matplotlib.pyplot as plt

import seaborn as sns

median_house = pd.read_csv('../input/fatal-police-shootings-in-the-us/MedianHouseholdIncome2015.csv', encoding='windows-1252')
percent_over = pd.read_csv('../input/fatal-police-shootings-in-the-us/PercentOver25CompletedHighSchool.csv', encoding='windows-1252')
percentage_people = pd.read_csv('../input/fatal-police-shootings-in-the-us/PercentagePeopleBelowPovertyLevel.csv', encoding='windows-1252')
kill = pd.read_csv('../input/fatal-police-shootings-in-the-us/PoliceKillingsUS.csv', encoding='windows-1252')
share_race = pd.read_csv('../input/fatal-police-shootings-in-the-us/ShareRaceByCity.csv', encoding='windows-1252')

percentage_people.poverty_rate.value_counts()

percentage_people.poverty_rate.replace(['-'], 0.0, inplace=True)
percentage_people.poverty_rate = percentage_people.poverty_rate.astype(float)
area_list = list(percentage_people['Geographic Area'].unique())
area_poverty_ratio = []
for i in area_list:
    x = percentage_people[percentage_people['Geographic Area'] == i]
    area_poverty_rate = sum(x.poverty_rate) / len(x)
    area_poverty_ratio.append(area_poverty_rate)
data = pd.DataFrame({'area_list': area_list, 'area_poverty_ratio': area_poverty_ratio})
new_index = data['area_poverty_ratio'].sort_values(ascending=False).index.values
sorted_data = data.reindex(new_index)
plt.xticks(rotation=45)

kill.name.value_counts()

separate = kill.name[kill.name != 'TK TK'].str.split()
a, b = zip(*separate)
name_list = a + b
name_count = Counter(name_list)
most_common_names = name_count.most_common(15)
x, y = zip(*most_common_names)
x, y = (list(x), list(y))

percent_over.percent_completed_hs.value_counts()

percent_over.percent_completed_hs.replace(['-'], 0.0, inplace=True)
percent_over.percent_completed_hs = percent_over.percent_completed_hs.astype(float)
area_list = list(percent_over['Geographic Area'].unique())
area_highschool = []
for i in area_list:
    x = percent_over[percent_over['Geographic Area'] == i]
    area_highschool_rate = sum(x.percent_completed_hs) / len(x)
    area_highschool.append(area_highschool_rate)
data = pd.DataFrame({'area_list': area_list, 'area_highschool_ratio': area_highschool})
new_index = data['area_highschool_ratio'].sort_values(ascending=True).index.values
sorted_data2 = data.reindex(new_index)
plt.xticks(rotation=45)

share_race.replace(['-'], 0.0, inplace=True)
share_race.replace(['(X)'], 0.0, inplace=True)
share_race.loc[:, ['share_white', 'share_black', 'share_native_american', 'share_asian', 'share_hispanic']] = share_race.loc[:, ['share_white', 'share_black', 'share_native_american', 'share_asian', 'share_hispanic']].astype(float)
area_list = list(share_race['Geographic area'].unique())
share_white = []
share_black = []
share_native_american = []
share_asian = []
share_hispanic = []
for i in area_list:
    x = share_race[share_race['Geographic area'] == i]
    share_white.append(sum(x.share_white) / len(x))
    share_native_american.append(sum(x.share_native_american) / len(x))
    share_black.append(sum(x.share_black) / len(x))
    share_asian.append(sum(x.share_asian) / len(x))
    share_hispanic.append(sum(x.share_hispanic) / len(x))
f, ax = plt.subplots(figsize=(9, 15))
sns.barplot(x=share_white, y=area_list, color='green', alpha=0.5, label='White')
sns.barplot(x=share_black, y=area_list, color='blue', alpha=0.5, label='Black')
sns.barplot(x=share_native_american, y=area_list, color='cyan', alpha=0.5, label='Native American')
sns.barplot(x=share_asian, y=area_list, color='yellow', alpha=0.5, label='Asian')
sns.barplot(x=share_hispanic, y=area_list, color='red', alpha=0.5, label='Hispanic')
ax.legend(loc='lower right', frameon=True)