-
Notifications
You must be signed in to change notification settings - Fork 24
/
pandas_bikes.qmd
557 lines (416 loc) · 14.5 KB
/
pandas_bikes.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
---
title: "Bike accident dataset"
---
**Disclaimer**: this course is adapted from the work [Pandas tutorial](https://github.com/jorisvandenbossche/pandas-tutorial/blob/master/01-pandas_introduction.ipynb) by Joris Van den Bossche. `R` users might also want to read [Pandas: Comparison with R / R libraries](https://pandas.pydata.org/docs/getting_started/comparison/comparison_with_r.html) for a smooth start in Pandas.
We start by importing the necessary libraries:
```{python}
%matplotlib inline
import os
import numpy as np
import calendar
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from cycler import cycler
import pooch # download data / avoid re-downloading
from IPython import get_ipython
import lzma # to process zip file
import plotly.express as px
sns.set_palette("colorblind")
palette = sns.color_palette("twilight", n_colors=12)
pd.options.display.max_rows = 8
```
**References**:
- [Data original source](https://www.data.gouv.fr/fr/datasets/accidents-de-velo-en-france/)
- [Possible visualization](https://koumoul.com/en/datasets/accidents-velos)
## Data loading and preprocessing
### Data loading
```{python}
# url = "https://koumoul.com/s/data-fair/api/v1/datasets/accidents-velos/raw"
url_db = "https://github.com/josephsalmon/HAX712X/raw/main/Data/accidents-velos_2022.csv.xz"
path_target = "./bicycle_db.csv.xz"
path, fname = os.path.split(path_target)
pooch.retrieve(url_db, path=path, fname=fname, known_hash=None)
with lzma.open(path_target) as f:
file_content = f.read().decode('utf-8')
# write the string file_content to a file named fname_uncompressed
with open("./bicycle_db.csv", 'w') as f:
f.write(file_content)
```
```{python}
df_bikes = pd.read_csv("bicycle_db.csv", na_values="", low_memory=False,
dtype={'data': str, 'heure': str, 'departement': str})
```
In June 2023, the author decided to change the name of the columns, hence we had to define a dictionary to come back to legacy names:
```{python}
new2old = {
"hrmn": "heure",
"secuexist": "existence securite",
"grav": "gravite accident",
"dep": "departement",
}
df_bikes.rename(columns=new2old, inplace=True)
```
```{python}
#| eval: false
get_ipython().system('head -5 ./bicycle_db.csv')
```
```{python}
pd.options.display.max_columns = 40
df_bikes.head()
```
```{python}
df_bikes['existence securite'].unique()
```
```{python}
df_bikes['gravite accident'].unique()
```
### Handle missing values
```{python}
df_bikes['date'].hasnans
```
```{python}
df_bikes['heure'].hasnans
```
So arbitrarily we fill missing values with 0 (since apparently there is no time 0 reported...to double check in the source.)
```{python}
df_bikes.fillna({'heure':'0'}, inplace=True)
```
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: start/end of the study
Can you find the starting day and the ending day of the study automatically?
[Hint]{.underline}: Sort the data! You can sort the data by time for instance, say with `df.sort('Time')`.
:::
### Date and time processing
Check the date/time format:
```{python}
df_bikes['date'] + ' ' + df_bikes['heure']
```
```{python}
time_improved = pd.to_datetime(
df_bikes["date"] + " " + df_bikes["heure"],
format="%Y-%m-%d %H",
errors="coerce",
)
```
```{python}
df_bikes["Time"] = time_improved
# remove rows with NaT
df_bikes.dropna(subset=["Time"], inplace=True)
# set new index
df_bikes.set_index("Time", inplace=True)
# remove useless columns
df_bikes.drop(columns=["heure", "date"], inplace=True)
```
```{python}
df_bikes.info()
```
```{python}
df_bike2 = df_bikes.loc[
:, ["gravite accident", "existence securite", "age", "sexe"]
]
df_bike2["existence securite"].replace({"Inconnu": np.nan}, inplace=True)
df_bike2.dropna(inplace=True)
```
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: Is the helmet saving your life?
Perform an analysis so that you can check the benefit or not of wearing a
helmet to save your life.
[Beware]{.underline}: Preprocessing is needed to use `pd.crosstab`, `pivot_table` to avoid
issues.
:::
```{python}
#| echo: false
group = df_bike2.pivot_table(
columns="existence securite",
index=["gravite accident", "sexe"],
aggfunc={"age": "count"},
margins=True,
)
```
```{python}
#| echo: false
pd.crosstab(
df_bike2["existence securite"],
df_bike2["gravite accident"],
normalize="index",
) * 100
```
```{python}
#| echo: false
pd.crosstab(
df_bike2["existence securite"],
df_bike2["gravite accident"],
values=df_bike2["age"],
aggfunc="count",
normalize="index",
) * 100
```
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: Are men and women dying equally on a bike?
Perform an analysis to check differences between men's and women's survival.
:::
```{python}
#| echo: false
idx_dead = df_bikes['gravite accident'] == '3 - Tué'
df_deads = df_bikes[idx_dead]
df_gravite = df_deads.groupby('sexe').size() / idx_dead.sum()
```
```{python}
#| echo: false
df_bikes.groupby('sexe').size() / df_bikes.shape[0]
```
```{python}
#| echo: false
pd.crosstab(
df_bike2["sexe"],
df_bike2["gravite accident"],
values=df_bike2["age"],
aggfunc="count",
normalize="columns",
margins=True,
) * 100
```
## Data visualization
Note that in the dataset, the information on the level of bike practice by gender is missing.
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: Accident during the week?
Perform an analysis to check when the accidents are occurring during the week.
:::
### Time series visualization
```{python}
df_bikes["weekday"] = df_bikes.index.day_of_week # Monday=0, Sunday=6
df_bikes.groupby(['weekday', df_bikes.index.hour])['sexe'].count()
```
```{python}
df_bikes.groupby(['weekday', df_bikes.index.hour])['age'].count()
```
The two last results are the same, no matter if you choose the `'age'` or `'sexe'` variable.
```{python}
#| layout-ncol: 1
#| echo: false
#| eval: false
accidents_week = (
df_bikes.groupby(["weekday", df_bikes.index.hour])["sexe"]
.count()
.unstack(level=0)
)
fig, axes = plt.subplots(1, 1, figsize=(7, 7))
accidents_week.plot(ax=axes)
axes.set_ylabel("Accidents")
axes.set_xlabel("Time of the day")
axes.set_title("Daily accident profile: monthly effect?")
axes.set_xticks(np.arange(0, 24))
axes.set_xticklabels(np.arange(0, 24), rotation=45)
axes.legend(
labels=[day for day in calendar.day_name],
loc="upper left",
)
plt.tight_layout()
plt.show()
```
Create a daily profile per day of the week:
```{python}
df_polar = (
df_bikes.groupby(["weekday", df_bikes.index.hour])["sexe"]
.count()
.reset_index()
) # all variable are similar in this sense, sexe could be replaced by age for instance here. XXX to simplify
df_polar = df_polar.astype({"Time": str}, copy=False)
df_polar["weekday"] = df_polar["weekday"].apply(lambda x: calendar.day_abbr[x])
df_polar.rename(columns={"sexe": "accidents"}, inplace=True)
```
Display these daily profiles
```{python}
n_colors = 8 # 7 days, but 8 colors help to have weekends days' color closer
colors = px.colors.sample_colorscale(
"mrybm", [n / (n_colors - 1) for n in range(n_colors)]
)
fig = px.line_polar(
df_polar,
r="accidents",
theta="Time",
color="weekday",
line_close=True,
range_r=[0, 600],
start_angle=0,
color_discrete_sequence=colors,
template="seaborn",
title="Daily accident profile: weekday effect?",
)
fig.show()
```
::: {.callout-note}
In `plotly` the figure is interactive. If you click on the legend on the right, you can select the day you want to see. It is very convenient to compare days two by two for instance.
:::
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: Accident during the year
Perform an analysis to check when the accidents are occurring during the week.
:::
Create a daily profile per month:
```{python}
#| layout-ncol: 1
df_bikes["month"] = df_bikes.index.month # Janvier=0, .... Decembre=11
# df_bikes['month'] = df_bikes['month'].apply(lambda x: calendar.month_abbr[x])
df_bikes.head()
df_bikes_month = (
df_bikes.groupby(["month", df_bikes.index.hour])["age"]
.count()
.unstack(level=0)
)
```
```{python}
#| layout-ncol: 1
#| echo: false
#| eval: false
fig, axes = plt.subplots(1, 1, figsize=(7, 7), sharex=True)
axes.set_prop_cycle(
(
cycler(color=palette)
+ cycler(ms=[4] * 12)
+ cycler(marker=["o", "^", "s", "p"] * 3)
+ cycler(linestyle=["-", "--", ":", "-."] * 3)
)
)
df_bikes_month.plot(ax=axes)
axes.set_ylabel("Accidents")
axes.set_xlabel("Time of the day")
axes.set_title("Daily accident profile: monthly effect?")
axes.set_xticks(np.arange(0, 24))
axes.set_xticklabels(np.arange(0, 24), rotation=45)
axes.legend(labels=calendar.month_name[1:], loc="upper left")
plt.tight_layout()
plt.show()
```
```{python}
df_polar2 = (
df_bikes.groupby(["month", df_bikes.index.hour])["sexe"]
.count()
.reset_index()
) # all variable are similar in this sense, sexe could be replaced by age for instance here. XXX to simplify
df_polar2 = df_polar2.astype({"Time": str}, copy=False)
df_polar2.rename(columns={"sexe": "accidents"}, inplace=True)
df_polar2["month"] = df_polar2["month"].apply(lambda x: calendar.month_abbr[x])
```
Display these daily profiles :
```{python}
# create a cyclical color scale for 12 values:
n_colors = 12
colors = px.colors.sample_colorscale(
"mrybm", [n / (n_colors - 1) for n in range(n_colors)]
)
fig = px.line_polar(
df_polar2,
r="accidents",
theta="Time",
color="month",
line_close=True,
range_r=[0, 410],
start_angle=0,
color_discrete_sequence=colors,
template="seaborn",
title="Daily accident profile: weekday effect?",
)
fig.show()
```
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: Accidents by department
Perform an analysis to check when the accidents are occurring for each department, relative to population size.
:::
### Geographic visualization
In this part, we will use the [geopandas](https://geopandas.org/) library to visualize the data on a map, along with `plotly` for interactivity.
```{python}
path_target = "./dpt_population.csv"
url = "https://public.opendatasoft.com/explore/dataset/population-francaise-par-departement-2018/download/?format=csv&timezone=Europe/Berlin&lang=en&use_labels_for_header=true&csv_separator=%3B"
path, fname = os.path.split(path_target)
pooch.retrieve(url, path=path, fname=fname, known_hash=None)
```
```{python}
df_dtp_pop = pd.read_csv("dpt_population.csv", sep=";", low_memory=False)
df_dtp_pop["Code Département"].replace("2A", "20A", inplace=True)
df_dtp_pop["Code Département"].replace("2B", "20B", inplace=True)
df_dtp_pop.sort_values(by=["Code Département"], inplace=True)
df_bikes["departement"].replace("2A", "20A", inplace=True)
df_bikes["departement"].replace("2B", "20B", inplace=True)
df_bikes.sort_values(by=["departement"], inplace=True)
# Clean extra departements
df_bikes = df_bikes.loc[
df_bikes["departement"].isin(df_dtp_pop["Code Département"].unique())
]
gd = df_bikes.groupby(["departement"], as_index=True, sort=True).size()
data = {"code": gd.index, "# Accidents per million": gd.values}
df = pd.DataFrame(data)
df["# Accidents per million"] = (
df["# Accidents per million"].values
* 10000.0
/ df_dtp_pop["Population"].values
)
```
We now need to download the `.geojson` file containing the geographic information for each department. We will use the `pooch` library to download the file and store it locally.
```{python}
path_target = "./departements.geojson"
# url = "https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements-avec-outre-mer.geojson"
url = "https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements-version-simplifiee.geojson"
path, fname = os.path.split(path_target)
pooch.retrieve(url, path=path, fname=fname, known_hash=None)
```
First, you have to handle Corsican departments, which are not in the same format as the others.
```{python}
#| layout-ncol: 1
import plotly.express as px
import geopandas
departement = geopandas.read_file("departements.geojson")
departement["code"].replace("2A", "20A", inplace=True)
departement["code"].replace("2B", "20B", inplace=True)
departement.sort_values(by=["code"], inplace=True)
a = ["0" + str(i) for i in range(1, 10)]
b = [str(i) for i in range(1, 10)]
dict_replace = dict(zip(a, b))
departement["code"].replace(dict_replace, inplace=True)
df["code"].replace(dict_replace, inplace=True)
departement["code"].replace("20A", "2A", inplace=True)
departement["code"].replace("20B", "2B", inplace=True)
df["code"].replace("20A", "2A", inplace=True)
df["code"].replace("20B", "2B", inplace=True)
departement.set_index("code", inplace=True)
print(departement['nom'].head(22))
```
Once this is done, you can plot the data on a map.
```{python}
fig = px.choropleth_mapbox(
df,
geojson=departement,
locations="code",
color="# Accidents per million",
range_color=(0, df["# Accidents per million"].max()),
color_continuous_scale="rdbu",
center={"lat": 44, "lon": 2},
zoom=3.55,
mapbox_style="white-bg",
)
fig.update_traces(selector=dict(type="choroplethmapbox"))
fig.update_layout(
title_text="Accidents per million inhabitants by department",
coloraxis_colorbar=dict(thickness=20, orientation="h", y=0.051, x=0.5),
)
fig.show()
```
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: Accidents by department
Perform an analysis to check when the accidents are occurring for each department, relative to the area of the *departements*.
:::
```{python}
#| echo: false
#| eval: false
path_target = "./dpt_area.csv"
url = "https://www.regions-et-departements.fr/fichiers/departements-francais.csv"
path, fname = os.path.split(path_target)
pooch.retrieve(url, path=path, fname=fname, known_hash=None)
```
::: {.callout-important appearance='default' icon="false"}
## EXERCISE: plot DOMs and metropolitan France (hard?)
Plot DOMs with metropolitan France, so that all departements are visible on the same figure (for instance using this [geoson](https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/regions-avec-outre-mer.geojson) file, and making all departements fitting a small figure).
:::
## References
- Other interactive tools for data visualization: Altair, Bokeh. See comparisons by Aarron Geller: [link](https://sites.northwestern.edu/researchcomputing/2022/02/03/what-is-the-best-interactive-plotting-package-in-python/)
- An interesting tutorial on Altair: [Altair introduction](https://infovis.fh-potsdam.de/tutorials/)