Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hypothesis: The mean house prices are higher in more populated areas. A higher population density ( people per square km ) results in a higher
Hypothesis:
The mean house prices are higher in more populated areas.
A higher population density
people per square km
results in a higher number of crimes committed.
The crime rate is higher in areas with low mean house prices.
Code:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Read data files
house
prices
df
pd
read
excel
MeanHousePricesClean
xlsx
crime
df
pd
read
excel
CrimeClean
xlsx
population
df
pd
read
excel
PopulationClean
xlsx
area
df
pd
read
excel
SuburbAreas
xlsx
header
None
# Transform area
df to long format
area
df
columns
area
df
iloc
# Set the first row as the header
area
df
area
df
:
# Remove the first row from the dataframe
area
df
area
df
set
index
Property
transpose
reset
index
area
df
columns
local
government
area', 'area
sq
km
# Convert 'area
sq
km
to numeric
area
df
area
sq
km
pd
to
numeric
area
df
area
sq
km
errors
'coerce'
# Rename columns in house
prices
df and crime
df to ensure consistent naming
house
prices
df
house
prices
df
rename
columns
Year
: 'year'
crime
df
crime
df
rename
columns
Year
: 'year', 'Local Government Area': 'local
government
area',
'Incidents recorded': 'incidents
recorded',
'Crime rate per
population: 'crime
rate'
population
df
population
df
rename
columns
Year
: 'year'
# Function to normalize LGA names
def normalize
lga
names
df
lga
column
:
if lga
column in df
columns:
df
lga
column
df
lga
column
astype
str
str
strip
str
replace
Shire
str
replace
City
str
strip
return df
# Normalize LGA names in all relevant DataFrames
house
prices
df
normalize
lga
names
house
prices
df
local
government
area'
crime
df
normalize
lga
names
crime
df
local
government
area'
for col in population
df
columns
:
:
population
df
normalize
lga
names
population
df
col
area
df
normalize
lga
names
area
df
local
government
area'
# Transform house
prices
df to long format
house
prices
long
df
pd
melt
house
prices
df
id
vars
year
var
name
'local
government
area', value
name
'house
price'
# Normalize 'local
government
area' column in house
prices
long
df
house
prices
long
df
normalize
lga
names
house
prices
long
df
local
government
area'
# Transform population
df to long format
population
long
df
pd
melt
population
df
id
vars
year
var
name
'local
government
area', value
name
'population'
# Normalize 'local
government
area' column in population
long
df
population
long
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started