PS - You need to have the CSV file uploaded in the jupyter notebook. Here is the link to the dataset - https://www.kaggle.com/greeshmagirish/crime-against-women-20012014-india
import jovian
jovian.commit(project='crime-against-women', environment=None)
!pip install pandas
!pip install matplotlib
!pip install seaborn
!pip install plotly
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
%matplotlib inline
jovian.commit('crimes_against_women.csv')
crimes_df = pd.read_csv('crimes_against_women.csv')
crimes_df.shape
import jovian
jovian.commit()
overall_crime = crimes_df.isna().sum()
overall_crime
districts = len(crimes_df.DISTRICT.unique())
districts
1605
crimes_df.drop(['DISTRICT', 'Unnamed: 0'], axis = 1, inplace=True)
crimes_df
print(crimes_df['STATE/UT'].unique())
['ANDHRA PRADESH' 'ARUNACHAL PRADESH' 'ASSAM' 'BIHAR' 'CHHATTISGARH' 'GOA'
'GUJARAT' 'HARYANA' 'HIMACHAL PRADESH' 'JAMMU & KASHMIR' 'JHARKHAND'
'KARNATAKA' 'KERALA' 'MADHYA PRADESH' 'MAHARASHTRA' 'MANIPUR' 'MEGHALAYA'
'MIZORAM' 'NAGALAND' 'ODISHA' 'PUNJAB' 'RAJASTHAN' 'SIKKIM' 'TAMIL NADU'
'TRIPURA' 'UTTAR PRADESH' 'UTTARAKHAND' 'WEST BENGAL' 'A & N ISLANDS'
'CHANDIGARH' 'D & N HAVELI' 'DAMAN & DIU' 'DELHI' 'LAKSHADWEEP'
'PUDUCHERRY' 'Andhra Pradesh' 'Arunachal Pradesh' 'Assam' 'Bihar'
'Chhattisgarh' 'Goa' 'Gujarat' 'Haryana' 'Himachal Pradesh'
'Jammu & Kashmir' 'Jharkhand' 'Karnataka' 'Kerala' 'Madhya Pradesh'
'Maharashtra' 'Manipur' 'Meghalaya' 'Mizoram' 'Nagaland' 'Odisha'
'Punjab' 'Rajasthan' 'Sikkim' 'Tamil Nadu' 'Tripura' 'Uttar Pradesh'
'Uttarakhand' 'West Bengal' 'A&N Islands' 'Chandigarh' 'D&N Haveli'
'Daman & Diu' 'Delhi UT' 'Lakshadweep' 'Puducherry' 'Telangana'
'A & N Islands']
# Fist we will remove all the repeated uppercase values
def remove_uppercase(r):
r = r['STATE/UT'].strip()
r = r.upper()
return r
crimes_df['STATE/UT'] = crimes_df.apply(remove_uppercase, axis=1)
#Now use replace function to replace the other type of repeated datas as dicussed above
crimes_df['STATE/UT'].replace("A&N ISLANDS", "A & N ISLANDS", inplace = True)
crimes_df['STATE/UT'].replace("D&N HAVELI", "D & N HAVELI", inplace = True)
crimes_df['STATE/UT'].replace("DELHI UT", "DELHI", inplace = True)
crimes_df['STATE/UT'].unique()
array(['ANDHRA PRADESH', 'ARUNACHAL PRADESH', 'ASSAM', 'BIHAR',
'CHHATTISGARH', 'GOA', 'GUJARAT', 'HARYANA', 'HIMACHAL PRADESH',
'JAMMU & KASHMIR', 'JHARKHAND', 'KARNATAKA', 'KERALA',
'MADHYA PRADESH', 'MAHARASHTRA', 'MANIPUR', 'MEGHALAYA', 'MIZORAM',
'NAGALAND', 'ODISHA', 'PUNJAB', 'RAJASTHAN', 'SIKKIM',
'TAMIL NADU', 'TRIPURA', 'UTTAR PRADESH', 'UTTARAKHAND',
'WEST BENGAL', 'A & N ISLANDS', 'CHANDIGARH', 'D & N HAVELI',
'DAMAN & DIU', 'DELHI', 'LAKSHADWEEP', 'PUDUCHERRY', 'TELANGANA'],
dtype=object)
len(crimes_df['STATE/UT'].unique())
36
import jovian
jovian.commit()
[jovian] Attempting to save notebook..
[jovian] Updating notebook "sathi-satb/crime-against-women" on https://jovian.ml/
[jovian] Uploading notebook..
[jovian] Capturing environment..
[jovian] Committed successfully! https://jovian.ml/sathi-satb/crime-against-women
victims_raped = crimes_df.Rape.sum()
victims_kidnapped_abducted = crimes_df.Kidnapping_Abduction.sum()
dowery_death = crimes_df.Dowry_Deaths.sum()
modesty_assault = crimes_df.Assault_for_her_modesty.sum()
insult_to_modesty = crimes_df.Insult_to_modesty_of_Women.sum()
domestic_violence = crimes_df.Domestic_violence.sum()
girls_imported = crimes_df.Importation_of_Girls.sum()
total_population_of_victim_overall = victims_raped + victims_raped + dowery_death +modesty_assault+ insult_to_modesty + domestic_violence+ girls_imported
total_population_of_victim_overall
5194570
fig, axes = plt.subplots(2, 3, figsize=(25, 12))
axes[0,0].set_title("Chart of rape cases in India in 2001-2014")
axes[0,0].bar(crimes_df.Year, crimes_df.Rape, color = 'black');
plt.xlabel('Year') #X-axis
plt.ylabel('Cases of Rape in India') #Y-axis
axes[0,1].set_title("Chart of Kidnapping and Abduction cases in India in 2001-2014")
axes[0,1].bar(crimes_df.Year, crimes_df.Kidnapping_Abduction, color = 'violet');
plt.xlabel('Year') #X-axis
plt.ylabel('Cases of Kidnappinga and Abduction in India') #Y-axis
axes[0,2].set_title("Chart of Dowry death cases in India in 2001-2014")
axes[0,2].bar(crimes_df.Year, crimes_df.Dowry_Deaths, color = 'navy');
plt.xlabel('Year') #X-axis
plt.ylabel('Cases of Dowry deaths in India') #Y-axis
axes[1,0].set_title("Chart of Assault to her modesty in 2001-2014")
axes[1,0].bar(crimes_df.Year, crimes_df.Assault_for_her_modesty, color = 'cyan');
plt.xlabel('Year') #X-axis
plt.ylabel('Cases of Assaulting a women for her modesty in India') #Y-axis
axes[1,1].set_title("Chart of Domestic Violence cases in India in 2001-2014")
axes[1,1].bar(crimes_df.Year, crimes_df.Domestic_violence, color = 'orange');
plt.xlabel('Year') #X-axis
plt.ylabel('Cases of Domestic Violance in India') #Y-axis
axes[1,2].set_title("Chart of Importation of girls in India in 2001-2014")
axes[1,2].bar(crimes_df.Year, crimes_df.Domestic_violence, color = 'red');
plt.xlabel('Year') #X-axis
plt.ylabel('Cases ofImportation of girls in India') #Y-axis
Text(0, 0.5, 'Cases ofImportation of girls in India')
count_df = crimes_df.groupby('Year')[['STATE/UT']].count()
count_df
import jovian
jovian.commit()
[jovian] Attempting to save notebook..
[jovian] Updating notebook "sathi-satb/crime-against-women" on https://jovian.ml/
[jovian] Uploading notebook..
[jovian] Capturing environment..
[jovian] Committed successfully! https://jovian.ml/sathi-satb/crime-against-women
crimes_df.drop(['Assault_for_her_modesty', 'Insult_to_modesty_of_Women'], axis = 1, inplace=True)
max_rape_cases = crimes_df.sort_values('Rape', ascending = False).head(10)
max_rape_cases
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-170-0de68700a3a3> in <module>
----> 1 crimes_df.drop(['Assault_for_her_modesty', 'Insult_to_modesty_of_Women'], axis = 1, inplace=True)
2 max_rape_cases = crimes_df.sort_values('Rape', ascending = False).head(10)
3 max_rape_cases
/srv/conda/envs/notebook/lib/python3.7/site-packages/pandas/core/frame.py in drop(self, labels, axis, index, columns, level, inplace, errors)
4167 level=level,
4168 inplace=inplace,
-> 4169 errors=errors,
4170 )
4171
/srv/conda/envs/notebook/lib/python3.7/site-packages/pandas/core/generic.py in drop(self, labels, axis, index, columns, level, inplace, errors)
3882 for axis, labels in axes.items():
3883 if labels is not None:
-> 3884 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
3885
3886 if inplace:
/srv/conda/envs/notebook/lib/python3.7/site-packages/pandas/core/generic.py in _drop_axis(self, labels, axis, level, errors)
3916 new_axis = axis.drop(labels, level=level, errors=errors)
3917 else:
-> 3918 new_axis = axis.drop(labels, errors=errors)
3919 result = self.reindex(**{axis_name: new_axis})
3920
/srv/conda/envs/notebook/lib/python3.7/site-packages/pandas/core/indexes/base.py in drop(self, labels, errors)
5276 if mask.any():
5277 if errors != "ignore":
-> 5278 raise KeyError(f"{labels[mask]} not found in axis")
5279 indexer = indexer[~mask]
5280 return self.delete(indexer)
KeyError: "['Assault_for_her_modesty' 'Insult_to_modesty_of_Women'] not found in axis"
max_dowry_death_cases = crimes_df.sort_values('Dowry_Deaths', ascending = False).head(10)
max_dowry_death_cases
max_domestic_violance_cases = crimes_df.sort_values('Domestic_violence', ascending = False).head(10)
max_domestic_violance_cases
max_importation_case = crimes_df.sort_values('Importation_of_Girls', ascending = False).head(10)
max_importation_case
counts_df = crimes_df.groupby('STATE/UT')[['Rape', 'Kidnapping_Abduction', 'Dowry_Deaths','Domestic_violence', 'Importation_of_Girls']].sum()
counts_df
counts_df.sort_values(by = 'Rape', ascending = False).head(5)
counts_df.sort_values(by = 'Kidnapping_Abduction', ascending = False).head(5)
counts_df.sort_values(by = 'Dowry_Deaths', ascending = False).head(5)
counts_df.sort_values(by = 'Domestic_violence', ascending = False).head(5)
counts_df.sort_values(by = 'Importation_of_Girls', ascending = False).head(5)
import jovian
jovian.commit()
[jovian] Attempting to save notebook..
[jovian] Updating notebook "sathi-satb/crime-against-women" on https://jovian.ml/
[jovian] Uploading notebook..
[jovian] Capturing environment..
[jovian] Committed successfully! https://jovian.ml/sathi-satb/crime-against-women
import jovian
jovian.commit()
[jovian] Attempting to save notebook..
import jovian
jovian.commit()