Skip to content

Analyzing Crime in Los Angeles

Initiated an effort to analyze crime data and derive insights, driven by a personal interest in crime stories and a desire to learn. The project aimed to support the Los Angeles Police Department (LAPD) in effectively allocating resources to address various crimes across different areas of the city.

# Import required libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Reading the crimes data from the CSV file, parsing dates, and specifying the TIME OCC column as a string
crimes = pd.read_csv("crimes.csv", parse_dates=["Date Rptd", "DATE OCC"], dtype={"TIME OCC": str})
crimes.info()
#Finding the frequencies of crimes by hour od occurrance
#Extracting the hour
crimes['HOUR OCC']=crimes['TIME OCC'].str[:2].astype(int)
#Plotting teh frequencies
sns.countplot(data=crimes,x='HOUR OCC')
#Storing the hour as a variable
peak_crime_hour = 12
#Identify the area with the most night Crime
# Subsetting for night hours
night_hours = [22, 23, 0, 1, 2, 3]
night_crimes = crimes[crimes['HOUR OCC'].isin(night_hours)]

# Counting crime by area
peak_night_crime_location = night_crimes.groupby('AREA NAME').size().sort_values(ascending=False).index[0]

print(peak_night_crime_location)
#Crimes by age group 
## Identify the number of crimes committed against victims by age group (0-17, 18-25, 26-34, 35-44, 45-54, 55-64, 65+) 

age_bins = [0, 17, 25, 34, 44, 54, 64, crimes["Vict Age"].max()]
age_labels = ["0-17", "18-25", "26-34", "35-44", "45-54", "55-64", "65+"]

# Add a new column using pd.cut() to bin values into discrete intervals
crimes["Age Bracket"] = pd.cut(crimes["Vict Age"],
                               bins=age_bins,
                               labels=age_labels)

# Find the category with the largest frequency
victim_ages = crimes["Age Bracket"].value_counts()
print(victim_ages)