# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import seaborn as sns;
import matplotlib.pyplot as plt;
from collections import Counter;

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
# Import data
medianHouseHoldInCome = pd.read_csv("../input/fatal-police-shootings-in-the-us/MedianHouseholdIncome2015.csv", encoding = "windows-1252");
percentagePeopleBelowPovertyLevel = pd.read_csv("../input/fatal-police-shootings-in-the-us/PercentagePeopleBelowPovertyLevel.csv", encoding = "windows-1252");
percentOver25CompletedHighSchool = pd.read_csv("../input/fatal-police-shootings-in-the-us/PercentOver25CompletedHighSchool.csv", encoding = "windows-1252");
shareRaceCity = pd.read_csv("../input/fatal-police-shootings-in-the-us/ShareRaceByCity.csv", encoding = "windows-1252");
kill = pd.read_csv("../input/fatal-police-shootings-in-the-us/PoliceKillingsUS.csv", encoding = "windows-1252");
percentagePeopleBelowPovertyLevel.head()
percentagePeopleBelowPovertyLevel.info()
print(percentagePeopleBelowPovertyLevel.poverty_rate.value_counts());
# Delete all poverty_rate = "-" items
percentagePeopleBelowPovertyLevel.poverty_rate.replace("-", 0.0, inplace = True);
percentagePeopleBelowPovertyLevel.poverty_rate.value_counts()
percentagePeopleBelowPovertyLevel.poverty_rate = percentagePeopleBelowPovertyLevel.poverty_rate.astype("float");
percentagePeopleBelowPovertyLevel.info()
# Get poverty rate of each state
areaList = list(percentagePeopleBelowPovertyLevel["Geographic Area"].unique());
print(areaList);
print("The number of states: {}".format(len(percentagePeopleBelowPovertyLevel["Geographic Area"].unique())));
areaPovertyRatio = [];
for each in areaList:
    stateFilter = percentagePeopleBelowPovertyLevel["Geographic Area"] == each
    currentState = percentagePeopleBelowPovertyLevel[stateFilter];
    areaPovertyRate = sum(currentState.poverty_rate) / len(currentState);
    areaPovertyRatio.append(areaPovertyRate);
data = pd.DataFrame({"areaList": areaList, "areaPovertyRatio": areaPovertyRatio});
newIndex = (data["areaPovertyRatio"].sort_values(ascending = False)).index.values; # Sıralanan değerlerin indisleri
sortedData = data.reindex(newIndex);

plt.figure(figsize=(15, 10));
ax = sns.barplot(x = sortedData.areaList, y = sortedData.areaPovertyRatio);
plt.xticks(rotation=90);
plt.xlabel("States");
plt.ylabel("Poverty Rate");
plt.title("Poverty Rata vs States");
sortedData.head()
# Find most 15 Name or Surname of the killed people
#print(kill.head());
#print(kill.name.value_counts());
nameFilter = kill.name != "TK TK";
separate = kill.name[nameFilter].str.split();
print(separate.value_counts());
a, b = zip(*separate);
#print(b);
nameList = (a+b);
#print(nameList);
nameCount = Counter(nameList);
#print(nameCount);
mostCommonNames = nameCount.most_common(15);
#print(mostCommonNames);
x, y = zip(*mostCommonNames);
#print(y)
x,y = list(x), list(y);

plt.figure(figsize = (15, 10));
ax = sns.barplot(x = x, y=y, palette=sns.cubehelix_palette(len(x)));
plt.xlabel("Name or Surname of the killed people");
plt.ylabel("Frequency");
plt.title("The most common names or surnames of the killed people");
print(percentOver25CompletedHighSchool.head())
print(percentOver25CompletedHighSchool.percent_completed_hs.value_counts());
percentOver25CompletedHighSchool.percent_completed_hs.replace("-", 0.0, inplace = True);
percentOver25CompletedHighSchool.percent_completed_hs = percentOver25CompletedHighSchool.percent_completed_hs.astype("float");
print(percentOver25CompletedHighSchool.info());

areaList = list(percentOver25CompletedHighSchool["Geographic Area"].unique());
print(areaList);
areaHighSchool = [];
for each in areaList:
    filterArea = percentOver25CompletedHighSchool["Geographic Area"] == each;
    x = percentOver25CompletedHighSchool[filterArea];
    rate = sum(x.percent_completed_hs) / len(x);
    areaHighSchool.append(rate);

# sorting
data = pd.DataFrame({"AreaList": areaList, "hsRate": areaHighSchool});
newIndices = (data.hsRate.sort_values(ascending = True)).index.values;
sortedData2 = data.reindex(newIndices);

# Visualization
plt.figure(figsize = (15, 10));
sns.barplot(x = sortedData.areaList, y = sortedData.areaPovertyRatio);
plt.xticks(rotation = 90);
plt.xlabel("States");
plt.ylabel("High School Rate");
plt.title("High School Rate vs States");

sortedData2.head()
"""
Percentage of state's population according to races which are black, white, native American, asian and hispanic
"""
#print(shareRaceCity.head());
print(shareRaceCity.info());
shareRaceCity.replace("-", 0.0, inplace = True);
shareRaceCity.replace("(X)", 0.0, inplace = True);
shareRaceCity.loc[:, ["share_white", "share_black", "share_native_american", "share_asian", "share_hispanic"]] = shareRaceCity.loc[:, ["share_white", "share_black", "share_native_american", "share_asian", "share_hispanic"]].astype("float");
shareRaceCity.info()
areaList = list(shareRaceCity["Geographic area"].unique());
print(areaList);
share_white = [];
share_black = [];
share_native_american = [];
share_asian = [];
share_hispanic = [];

for each in areaList:
    currentAreaFilter = (shareRaceCity["Geographic area"] == each);
    share_white.append(sum(shareRaceCity[currentAreaFilter].share_white) / len(shareRaceCity[currentAreaFilter].share_white));
    share_black.append(sum(shareRaceCity[currentAreaFilter].share_black) / len(shareRaceCity[currentAreaFilter].share_black));
    share_native_american.append(sum(shareRaceCity[currentAreaFilter].share_native_american) / len(shareRaceCity[currentAreaFilter].share_native_american));
    share_asian.append(sum(shareRaceCity[currentAreaFilter].share_asian) / len(shareRaceCity[currentAreaFilter].share_asian));
    share_hispanic.append(sum(shareRaceCity[currentAreaFilter].share_hispanic) / len(shareRaceCity[currentAreaFilter].share_hispanic));
# visualization
f,ax = plt.subplots(figsize = (9,15))
sns.barplot(x=share_white,y=areaList,color='green',alpha = 0.5,label='White' )
sns.barplot(x=share_black,y=areaList,color='blue',alpha = 0.7,label='African American')
sns.barplot(x=share_native_american,y=areaList,color='cyan',alpha = 0.6,label='Native American')
sns.barplot(x=share_asian,y=areaList,color='yellow',alpha = 0.6,label='Asian')
sns.barplot(x=share_hispanic,y=areaList,color='red',alpha = 0.6,label='Hispanic')

ax.legend(loc='lower right',frameon = True)     # legendlarin gorunurlugu
ax.set(xlabel='Percentage of Races', ylabel='States',title = "Percentage of State's Population According to Races ")

"""
Point Plot
"""
# sortedData = areaPovertyRatio
# sortedData2 = areaHighSchool

sortedData.areaPovertyRatio = sortedData.areaPovertyRatio / max(sortedData.areaPovertyRatio);
sortedData2.hsRate = sortedData2.hsRate / max(sortedData2.hsRate);
data = pd.concat([sortedData, sortedData2["hsRate"]], axis = 1);
data.sort_values("areaPovertyRatio", inplace = True);

# Visualization
f, ax1 = plt.subplots(figsize = (20, 10));
sns.pointplot(x = "areaList", y = "areaPovertyRatio", data = data, color = "lime", alpha = 0.8);
sns.pointplot(x = "areaList", y = "hsRate", data = data, color = "red", alpha = 0.8);
plt.text(40, 0.6, "HS Graduate Ratio", color = "red", fontsize = 17, style = "italic");
plt.text(40, 0.55, "Poverty Ratio", color = "lime", fontsize = 17, style = "italic");
plt.xlabel("States", fontsize = 15, color = "blue");
plt.ylabel("Values", fontsize = 15, color = "blue");
plt.title("High School Gradate Rate vs Poverty Rate", fontsize = 20, color = "blue");
plt.grid();