-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_3.py
More file actions
97 lines (85 loc) · 3.42 KB
/
Copy path_3.py
File metadata and controls
97 lines (85 loc) · 3.42 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv('ENCDM_Menage_2014.csv')
# Sous-groupes niveau 2 de G1 (alimentation)
# et les catégories "signal fort" identifiées
focus = {
# G1 - Alimentation détaillée
'Céréales': 'DAP_G01',
'Viandes': 'DAP_G02',
'Poissons': 'DAP_G03',
'Huiles_graisses': 'DAP_G04',
'Lait_oeufs': 'DAP_G05',
'Fruits': 'DAP_G06',
'Légumes': 'DAP_G07',
'Sucre_confis': 'DAP_G08',
'Café_thé': 'DAP_G09',
# G6 - Santé
'Medicaments': 'DAP_G61',
'Soins_ambul': 'DAP_G62',
'Hospitalisation': 'DAP_G63',
# G9 - Resto/Hotels
'Restaurants': 'DAP_G91',
'Hotels_voyages': 'DAP_G92',
# G7 - Transport
'Transport_perso': 'DAP_G71',
'Transport_public':'DAP_G72',
}
print("=== Coefficients budgétaires sous-catégories (%) ===\n")
for label, col in focus.items():
if col in df.columns:
df[f'CB_{col}'] = df[col] / df['DAP'] * 100
means = df.groupby('Quintiles')[f'CB_{col}'].mean()
variation = means[5.0] - means[1.0]
direction = "↗" if variation > 0 else "↘"
print(f"{label:<20} Q1={means[1.0]:.2f}% Q5={means[5.0]:.2f}% "
f"Δ={variation:+.2f}% {direction}")
else:
print(f"{label:<20} colonne {col} introuvable")
# Visualisation : Q1 vs Q5 side by side
labels_found = []
q1_vals, q5_vals = [], []
for label, col in focus.items():
cb_col = f'CB_{col}'
if cb_col in df.columns:
means = df.groupby('Quintiles')[cb_col].mean()
labels_found.append(label)
q1_vals.append(means[1.0])
q5_vals.append(means[5.0])
x = np.arange(len(labels_found))
width = 0.35
fig, ax = plt.subplots(figsize=(14, 6))
bars1 = ax.bar(x - width/2, q1_vals, width, label='Q1 (précaires)',
color='#d62728', alpha=0.85)
bars2 = ax.bar(x + width/2, q5_vals, width, label='Q5 (aisés)',
color='#9467bd', alpha=0.85)
ax.set_xlabel('Catégorie de dépense')
ax.set_ylabel('% du budget total')
ax.set_title('Structure du panier : Q1 (précaires) vs Q5 (aisés)\nSous-catégories clés')
ax.set_xticks(x)
ax.set_xticklabels(labels_found, rotation=45, ha='right')
ax.legend()
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: f'{y:.1f}%'))
plt.tight_layout()
plt.savefig('q1_vs_q5_detail.png', dpi=150, bbox_inches='tight')
plt.show()
print("\nGraphique sauvegardé !")
# === Coefficients budgétaires sous-catégories (%) ===
# Céréales Q1=9.54% Q5=4.87% Δ=-4.66% ↘
# Viandes Q1=3.06% Q5=3.16% Δ=+0.09% ↗
# Poissons Q1=6.38% Q5=3.32% Δ=-3.05% ↘
# Huiles_graisses Q1=10.85% Q5=8.00% Δ=-2.85% ↘
# Lait_oeufs Q1=1.36% Q5=1.46% Δ=+0.10% ↗
# Fruits Q1=4.95% Q5=2.32% Δ=-2.63% ↘
# Légumes Q1=2.29% Q5=1.18% Δ=-1.12% ↘
# Sucre_confis Q1=2.20% Q5=2.77% Δ=+0.57% ↗
# Café_thé Q1=2.32% Q5=1.06% Δ=-1.26% ↘
# Medicaments Q1=0.91% Q5=5.69% Δ=+4.78% ↗
# Soins_ambul Q1=1.96% Q5=1.72% Δ=-0.23% ↘
# Hospitalisation Q1=1.33% Q5=2.45% Δ=+1.12% ↗
# Restaurants Q1=0.23% Q5=1.35% Δ=+1.12% ↗
# Hotels_voyages Q1=0.71% Q5=2.33% Δ=+1.62% ↗
# Transport_perso Q1=0.20% Q5=0.59% Δ=+0.39% ↗
# Transport_public Q1=0.06% Q5=0.12% Δ=+0.06% ↗
# Graphique sauvegardé !