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 load_data(filepath):
with open(filepath,'r') as f:
return json.load(f)
def accum_crime(unit, crimes, city_dicts_lst):
result ={}
# Convert single crime to list for consistent processing
if isinstance(crimes, str):
crimes =[crimes]
# Accumulate counts
for city in city_dicts_lst:
key = city[unit]
if key not in result:
result[key]=0
for crime in crimes:
result[key]+= city[crime]
return result
def create_crime_summary(city_dicts_lst):
# Define crime categories
violent_crimes =['Murder', 'Rape', 'Robbery', 'Assault']
nonviolent_crimes =['Burglary', 'Theft', 'Vehicle_Theft']
# Create summaries
murder_by_region = accum_crime('Region', 'Murder', city_dicts_lst)
violent_by_region = accum_crime('Region', violent_crimes, city_dicts_lst)
nonviolent_by_region = accum_crime('Region', nonviolent_crimes, city_dicts_lst)
return murder_by_region, violent_by_region, nonviolent_by_region
def plot_crime_data(data_dict, title, figsize=(10,6)):
# Convert dictionary to dataframe
df = pd.DataFrame(list(data_dict.items()), columns=['Region', 'Incidents'])
# Define colors for regions
color_map ={
'South': 'blue', 'West': 'orange',
'Northeast': 'green', 'Midwest': 'red'
}
colors =[color_map[region] for region in df['Region']]
# Create plot
plt.figure(figsize=figsize)
plt.bar(df['Region'], df['Incidents'], color=colors)
plt.title(title)
plt.xlabel('Region')
plt.ylabel('Incidents')
# Print data table
print(f"
-->{title}--")
print(df.to_string(index=True))
print()
plt.show()
def create_state_summary(city_dicts_lst):
# Get violent crime totals
violent_crimes =['Murder', 'Rape', 'Robbery', 'Assault']
state_crimes = accum_crime('State', violent_crimes, city_dicts_lst)
# Calculate national average (51 states including DC)
national_avg = sum(state_crimes.values())/51
# Create summary with differences
summary ={
state: {
'Crimes': crimes,
'Diff from Avg': round(crimes - national_avg)
}
for state, crimes in state_crimes.items()
}
# Print formatted table
print(f"
--> National Average Violent Crime: {int(national_avg)}--")
print("
State Crimes Diff from Avg")
print("--------------------------------------------")
for state in sorted(summary.keys()):
print(f"{state:15}{summary[state]['Crimes']:>8}{summary[state]['Diff from Avg']:>14}")
def main():
# Filepath to the JSON data
filepath = "FBI_CrimeData_2016.json"
# Load data
city_dicts_lst = load_data(filepath)
# Create and display all visualizations and summaries
murder_by_region, violent_by_region, nonviolent_by_region = create_crime_summary(city_dicts_lst)
plot_crime_data(murder_by_region, "Murder by Region")
plot_crime_data(violent_by_region, "Violent Crime by Region")
plot_crime_data(nonviolent_by_region, "Non-Violent Crime by Region")
create_state_summary(city_dicts_lst)
# Call the main function
if __name__=="__main__":
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)
/tmp/ipykernel_75272/1618424347.py in
87 # Call the main function
88 if _name == "main":
--->89
main()
/tmp/ipykernel_75272/1618424347.py in main()
79 city_dicts_lst = load_data(filepath)
80 # Create and display all visualizations and summaries
--->81 murder_by_region, violent_by_region, nonviolent_by_region = create_crime_summary(city_dicts_lst)
82 plot_crime_data(murder_by_region, "Murder by Region")
83 plot_crime_data(violent_by_region, "Violent Crime by Region")
/tmp/ipykernel_75272/1618424347.py in create_crime_summary(city_dicts_lst)
26 nonviolent_crimes =['Burglary', 'Theft', 'Vehicle_Theft']
27 # Create summaries
--->28 murder_by_region = accum_crime('Region', 'Murder', city_dicts_lst)
29 violent_by_region = accum_crime('Region', violent_crimes, city_dicts_lst)
30 nonviolent_by_region = acum_crime('Region', nonviolent_crimes, city_dicts_lst)
/tmp/ipykernel_75272/1618424347.py in accum_crime(unit, crimes, city_dicts_lst)
18 result[key]=0
19 for crime in crimes:
--->20 result[key]+= city[crime]
21 return result
22
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
import json import pandas as pd import

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!