Question: import json import pandas as pd import matplotlib.pyplot as plt def load _ data ( filepath ) : with open ( filepath , ' r
import json
import pandas as pd
import matplotlib.pyplot as plt
def loaddatafilepath:
with openfilepathr as f:
return json.loadf
def accumcrimeunit crimes, citydictslst:
result
# Convert single crime to list for consistent processing
if isinstancecrimes str:
crimes crimes
# Accumulate counts
for city in citydictslst:
key cityunit
if key not in result:
resultkey
for crime in crimes:
resultkey citycrime
return result
def createcrimesummarycitydictslst:
# Define crime categories
violentcrimes Murder 'Rape', 'Robbery', 'Assault'
nonviolentcrimes Burglary 'Theft', 'VehicleTheft'
# Create summaries
murderbyregion accumcrimeRegion 'Murder', citydictslst
violentbyregion accumcrimeRegion violentcrimes, citydictslst
nonviolentbyregion accumcrimeRegion nonviolentcrimes, citydictslst
return murderbyregion, violentbyregion, nonviolentbyregion
def plotcrimedatadatadict, title, figsize:
# Convert dictionary to dataframe
df pdDataFramelistdatadict.items columnsRegion 'Incidents'
# Define colors for regions
colormap
'South': 'blue', 'West': 'orange',
'Northeast': 'green', 'Midwest': 'red'
colors colormapregion for region in dfRegion
# Create plot
pltfigurefigsizefigsize
pltbardfRegion dfIncidents colorcolors
plttitletitle
pltxlabelRegion
pltylabelIncidents
# Print data table
printf
title
printdftostringindexTrue
print
pltshow
def createstatesummarycitydictslst:
# Get violent crime totals
violentcrimes Murder 'Rape', 'Robbery', 'Assault'
statecrimes accumcrimeState violentcrimes, citydictslst
# Calculate national average states including DC
nationalavg sumstatecrimes.values
# Create summary with differences
summary
state:
'Crimes': crimes,
'Diff from Avg': roundcrimes nationalavg
for state, crimes in statecrimes.items
# Print formatted table
printf
National Average Violent Crime: intnationalavg
print
State Crimes Diff from Avg"
print
for state in sortedsummarykeys:
printfstate:summarystateCrimes:summarystateDiff from Avg':
def main:
# Filepath to the JSON data
filepath "FBICrimeDatajson"
# Load data
citydictslst loaddatafilepath
# Create and display all visualizations and summaries
murderbyregion, violentbyregion, nonviolentbyregion createcrimesummarycitydictslst
plotcrimedatamurderbyregion, "Murder by Region"
plotcrimedataviolentbyregion, "Violent Crime by Region"
plotcrimedatanonviolentbyregion, "NonViolent Crime by Region"
createstatesummarycitydictslst
# Call the main function
if namemain:
main
Below is the image of the error im getting, also is there any way to open up a conversation with people? I had another question for the last person who answered me
The photo
TypeError
Traceback most recent call last
tmpipykernelpy in
# Call the main function
if name "main":
main
tmpipykernelpy in main
citydictslst loaddatafilepath
# Create and display all visualizations and summaries
murderbyregion, violentbyregion, nonviolentbyregion createcrimesummarycitydictslst
plotcrimedatamurderbyregion, "Murder by Region"
plotcrimedataviolentbyregion, "Violent Crime by Region"
tmpipykernelpy in createcrimesummarycitydictslst
nonviolentcrimes Burglary 'Theft', 'VehicleTheft'
# Create summaries
murderbyregion accumcrimeRegion 'Murder', citydictslst
violentbyregion accumcrimeRegion violentcrimes, citydictslst
nonviolentbyregion acumcrimeRegion nonviolentcrimes, citydictslst
tmpipykernelpy in accumcrimeunit crimes, citydictslst
resultkey
for crime in crimes:
resultkey citycrime
return result
TypeError: unsupported operand types for : 'int' and str
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
