# Start writing code here...
SELECT *
FROM 'activities.csv'
Importing Packages
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import datetime
warnings off
df_activities.head()
Creating Suitable DataFrame
df_activities= df_activities[['Activity ID','Activity Date', 'Activity Type', 'Distance', 'Moving Time', 'Average Speed', 'Max Speed']]
df_runs= df_activities[df_activities['Activity Type'] == 'Run']
df_runs['Distance']= df_runs['Distance'].astype(float)
df_runs['Activity Date']= pd.to_datetime(df_runs['Activity Date'])
df_runs['Activity Date']= df_runs['Activity Date'].dt.strftime('%Y-%m-%d')
df_runs['Activity Date']= pd.to_datetime(df_runs['Activity Date'])
df_runs.set_index('Activity Date', inplace= True)
df_runs.index.freq= pd.infer_freq(df_runs.index)
df_runs['pace']= (df_runs['Moving Time']/60)/df_runs['Distance']
df_runs['speed']= df_runs['Distance']/(df_runs['Moving Time']/3600)
Plotting
df_runs_counts_monthly= df_runs['Activity ID'].resample(rule= 'M').count()
df_runs_distance_monthly= df_runs['Distance'].resample(rule= 'M').sum()
df_runs_speed_monthly= df_runs['speed'].resample(rule= 'M').median()
size= df_runs_distance_monthly.values
fig= go.Figure()
fig.add_trace(go.Scatter(
x= df_runs_counts_monthly.index,
y= df_runs_counts_monthly.values,
mode= 'markers',
marker= dict(
size= size,
color= df_runs_speed_monthly.values,
sizemode= 'area',
showscale= True,
colorbar= dict(title= 'Speed'),
colorscale= 'speed',
),
name= '# of Runs per Month')
)
fig.add_vline(x= '2018-09-15', line_dash= 'dash', line_color= 'green', name= 'Start Running')
fig.add_annotation(
x= '2018-09-15',
y= 25,
text= "Start Running",
showarrow= False,
bgcolor= '#98BF64',
font= dict(color= 'white')
)
fig.add_vline(x= '2020-02-15', line_dash= 'dash', line_color= 'red', name= 'Corona Catastrophie')
fig.add_annotation(
x= '2020-02-29',
y= 22,
text= "Corona Catastrophie",
showarrow= False,
bgcolor= '#655967',
font= dict(color= 'white')
)
fig.add_vline(x= '2020-08-15', line_dash= 'dash', line_color= 'orange', name= 'My personal life 1st big issue')
fig.add_annotation(
x= '2020-08-31',
y= 25,
text= "My personal life 1st big issue",
showarrow= False,
bgcolor= '#655967',
font= dict(color= 'white')
)
fig.add_vline(x= '2021-09-15', line_dash= 'dash', line_color= 'yellow', name= 'My personal life 2nd big issue')
fig.add_annotation(
x= '2021-09-30',
y= 22,
text= "My personal life 2nd big issue",
showarrow= False,
bgcolor= '#655967',
font= dict(color= 'white')
)
fig.update_layout(title= 'Monthly Runs', template= 'plotly_dark',
legend= dict())
fig.show()
df_runs_counts_yearly= df_runs['Activity ID'].resample(rule= 'AS').count()
fig= go.Figure()
fig.add_trace(go.Bar(
x= df_runs_counts_yearly.index,
y= df_runs_counts_yearly.values,
marker= dict(color= 'red'),
text= df_runs_counts_yearly.values,
textposition= 'outside'
))
fig.update_layout(title= 'Runs per Year', template= 'plotly_dark',
legend= dict(), height= 500)
fig.show()