Week 6 Demo
In [1]:
Copied!
notebook_path = Path(os.path.abspath("__file__")).parent
os.chdir(notebook_path)
os.getcwd()
notebook_path = Path(os.path.abspath("__file__")).parent
os.chdir(notebook_path)
os.getcwd()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 1 ----> 1 notebook_path = Path(os.path.abspath("__file__")).parent 2 os.chdir(notebook_path) 3 os.getcwd() NameError: name 'Path' is not defined
In [9]:
Copied!
import pandas as pd
data = pd.read_csv('./Worcester_Police_Use_of_Force_Incidents_(July_2024).csv')
# Convert 'Date___Time' column to datetime format
data['Date___Time'] = pd.to_datetime(data['Date___Time'], format='%m/%d/%y %I:%M %p')
# Create a new column for the date
data['Date'] = data['Date___Time'].dt.date
# Group data by date and count incidents per day
incidents_per_day = data.groupby('Date').size()
import pandas as pd
data = pd.read_csv('./Worcester_Police_Use_of_Force_Incidents_(July_2024).csv')
# Convert 'Date___Time' column to datetime format
data['Date___Time'] = pd.to_datetime(data['Date___Time'], format='%m/%d/%y %I:%M %p')
# Create a new column for the date
data['Date'] = data['Date___Time'].dt.date
# Group data by date and count incidents per day
incidents_per_day = data.groupby('Date').size()
In [3]:
Copied!
import plotly.express as px
import pandas as pd
# Sample time series data
# Assuming 'incidents_per_day' is a Pandas Series with a DateTime index
# Creating some mock data for this example
date_rng = pd.date_range(start='2023-01-01', end='2023-12-31', freq='D')
# Create the line plot
fig = px.line(x=incidents_per_day.index,
y=incidents_per_day.values,
labels={'x': 'Date', 'y': 'Incidents'},
title='Interactive Time Series with Date Range Selector',
line_shape='spline')
# Add a range slider for selecting the time range
fig.update_layout(
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=1, label="1 Month", step="month", stepmode="backward"),
dict(count=6, label="6 Months", step="month", stepmode="backward"),
dict(count=1, label="1 Year", step="year", stepmode="backward"),
dict(step="all", label="All Time")
])
),
rangeslider=dict(visible=True), # Show the range slider below the x-axis
type="date"
)
)
# Show the plot
fig.show()
import plotly.express as px
import pandas as pd
# Sample time series data
# Assuming 'incidents_per_day' is a Pandas Series with a DateTime index
# Creating some mock data for this example
date_rng = pd.date_range(start='2023-01-01', end='2023-12-31', freq='D')
# Create the line plot
fig = px.line(x=incidents_per_day.index,
y=incidents_per_day.values,
labels={'x': 'Date', 'y': 'Incidents'},
title='Interactive Time Series with Date Range Selector',
line_shape='spline')
# Add a range slider for selecting the time range
fig.update_layout(
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=1, label="1 Month", step="month", stepmode="backward"),
dict(count=6, label="6 Months", step="month", stepmode="backward"),
dict(count=1, label="1 Year", step="year", stepmode="backward"),
dict(step="all", label="All Time")
])
),
rangeslider=dict(visible=True), # Show the range slider below the x-axis
type="date"
)
)
# Show the plot
fig.show()