Skip to content

Commit

Permalink
[tg] - sous-onglet matériaux : filtrer sur milieux avec plus de 3 col…
Browse files Browse the repository at this point in the history
…lectes
  • Loading branch information
tgazagnes committed May 29, 2024
1 parent 9f1b0e4 commit 0835d03
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions dashboards/app/pages/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,28 @@ def carac_exclusions(df):

df_other["Exclusions"] = df_other.apply(lambda row: carac_exclusions(row), axis=1)

# Raccourcir les étiquettes de milieux trop longues
df_other = df_other.replace(
{
"Zone naturelle ou rurale (hors littoral et montagne)": "Zone naturelle ou rurale"
}
)

# Copier le df pour la partie filtrée par milieu/lieu/année
df_other_metrics_raw = df_other.copy()

# Fonction pour améliorer l'affichage des nombres (milliers, millions, milliards)
def french_format(x: int) -> str:
if x > 1e9:
y = x / 1e9
y = locale.format("%.2f", y, grouping=True)
y = locale.format_string("%.2f", y, grouping=True)
return f"{y} milliards"
if x > 1e6:
y = x / 1e6
y = locale.format("%.2f", y, grouping=True)
y = locale.format_string("%.2f", y, grouping=True)
return f"{y} millions"
else:
y = locale.format("%.0f", x, grouping=True)
y = locale.format_string("%.0f", x, grouping=True)
return f"{y}"

# 3 Onglets : Matériaux, Top déchets, Filières et marques
Expand Down Expand Up @@ -347,9 +354,25 @@ def french_format(x: int) -> str:
# Affichage du graphique
st.plotly_chart(fig2, use_container_width=True)

# Ligne 3 : Graphe par milieu de collecte
### GRAPHIQUE PAR MILIEU DE COLLECTE

# Calcul du nombre de collectes par milieu
df_nb_par_milieu = (
df_other.groupby("TYPE_MILIEU", as_index=True)
.agg(
{
"ID_RELEVE": "count",
}
)
.sort_values("TYPE_MILIEU", ascending=True)
)
# Exclure les milieux avec moins de 3 collectes
milieux_a_exclure = df_nb_par_milieu[
df_nb_par_milieu["ID_RELEVE"] <= 3
].index.to_list()
df_nb_par_milieu = df_nb_par_milieu.drop(milieux_a_exclure, axis=0)

# Grouper par année et type de matériau
# Calcul du dataframe groupé par milieu et matériau pour le graphique
df_typemilieu = df_volume_cleaned.groupby(
["TYPE_MILIEU", "Matériau"], as_index=False
).agg({"Volume_m3": "sum", "ID_RELEVE": "count"})
Expand All @@ -358,12 +381,10 @@ def french_format(x: int) -> str:
["TYPE_MILIEU", "Volume_m3"], ascending=True
)

# Raccourcir les étiquettes trop longues
df_typemilieu = df_typemilieu.replace(
{
"Zone naturelle ou rurale (hors littoral et montagne)": "Zone naturelle ou rurale"
}
)
# Retirer milieux avec moins de 3 collectes
df_typemilieu = df_typemilieu[
~df_typemilieu["TYPE_MILIEU"].isin(milieux_a_exclure)
]

# Ne pas faire apparaître la catégorie "Multi-lieux"
lignes_multi = df_typemilieu.loc[df_typemilieu["TYPE_MILIEU"] == "Multi-lieux"]
Expand Down Expand Up @@ -414,17 +435,6 @@ def french_format(x: int) -> str:
with st.container(border=True):
st.plotly_chart(fig3, use_container_width=True)

# Afficher un tableau du nombre de collectes par milieu en dessous
df_nb_par_milieu = (
df_other.groupby("TYPE_MILIEU", as_index=True)
.agg(
{
"ID_RELEVE": "count",
}
)
.sort_values("TYPE_MILIEU", ascending=True)
)

# Ne pas faire apparaître la catégorie "Multi-lieux"
lignes_multi = df_nb_par_milieu.loc[df_nb_par_milieu.index == "Multi-lieux"]
df_nb_par_milieu.drop(lignes_multi.index, axis=0, inplace=True)
Expand All @@ -447,7 +457,8 @@ def french_format(x: int) -> str:
st.table(df_nb_par_milieu.T)
st.caption(
f"Les ramassages catégorisés en 'Multi-lieux' "
+ f"ont été retirés de l'analyse."
+ f"ont été retirés de l'analyse. "
+ f"Les milieux représentant moins de 3 ramassages ne sont pas affichés."
)

# Ligne 3 : Graphe par milieu , lieu et année
Expand Down

0 comments on commit 0835d03

Please sign in to comment.