-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdc.py
More file actions
executable file
·73 lines (46 loc) · 1.8 KB
/
dc.py
File metadata and controls
executable file
·73 lines (46 loc) · 1.8 KB
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
#!/usr/bin/env python
import pandas as pd
import matplotlib.pyplot as plt
# https://www.insee.fr/fr/statistiques/4487988
# https://www.insee.fr/fr/statistiques/fichier/4487988/2020-10-02_detail.zip
def compute_dc_j():
dc = pd.DataFrame()
dc = dc.append(pd.read_csv(f"insee_dc/DC_2018_det.csv", sep=";"))
dc = dc.append(pd.read_csv(f"insee_dc/DC_2019_det.csv", sep=";"))
dc = dc.append(pd.read_csv(f"insee_dc/DC_2020_det.csv", sep=";"))
dc['depdom'] = dc.COMDOM.astype(str).apply(
lambda x: x[0:2 if x[0:2] != "97" else 3])
dc_j = dc.groupby(['depdom', 'ADEC', 'MDEC', 'JDEC']).ANAIS.count()
return dc_j.rename("dc_j")
def graph(dc_j, selection):
depts = [f"{dep:02}" for dep in selection]
p = pd.DataFrame({dep: dc_j[dep] for dep in depts}, index=range(1,13))
with plt.xkcd():
plot(p).set_xlim(-0.5, 5.7)
plot(p).set_xlim(5.5, 11.7)
def plot(p):
_ = p.plot.bar(rot=0)
_.set_xlabel('MOIS', labelpad=14)
_.set_ylabel('DC / JOUR', labelpad=14)
_.legend(handlelength=1, markerfirst=False)
_.set_ylim(1)
_.figure.set(figheight=7, figwidth=12)
return _
def alt_compute_and_graph(dc_m, days_in_month):
z = pd.DataFrame([
[ dep, month, round(dc_m[(dep,month)] / days_in_month[month]) ]
for dep, month in dc_m.index ],
columns=['depdom', 'mois', 'dc_j'])
p = z.pivot(index='mois', columns='depdom')
p[[('dc_j', dep) for dep in depts]] \
.plot.bar(ylabel='dc / jour') \
.legend(depts)
def main():
dc_j = compute_dc_j()
with open("dc_j.csv", "w") as f:
f.write(dc_j.to_csv())
return
dc_j = dc_j.groupby(['depdom', 'MDEC']).mean()
graph(dc_j, [13, 33, 69, 83, 31, 38, 1, 26, 73])
plt.show()
main()