mirror of
https://github.com/guezoloic/millesima_projetS6.git
synced 2026-03-28 19:13:42 +00:00
ajout : remplac les notes manquantes par la moyenne de l'appellation
This commit is contained in:
32
cleaning.py
32
cleaning.py
@@ -1,9 +1,12 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from pandas import DataFrame, to_numeric
|
from pandas import DataFrame, to_numeric
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
SCORE_COLS = ["Robert", "Robinson", "Suckling"]
|
||||||
|
|
||||||
|
|
||||||
def display_info(df: DataFrame) -> None:
|
def display_info(df: DataFrame) -> None:
|
||||||
print(df.all())
|
df.describe()
|
||||||
print(df.info())
|
print(df.info())
|
||||||
print("\nNombre de valeurs manquantes par colonne :")
|
print("\nNombre de valeurs manquantes par colonne :")
|
||||||
print(df.isna().sum())
|
print(df.isna().sum())
|
||||||
@@ -45,4 +48,29 @@ def mean_robinson(df: DataFrame) -> DataFrame:
|
|||||||
|
|
||||||
|
|
||||||
def mean_suckling(df: DataFrame) -> DataFrame:
|
def mean_suckling(df: DataFrame) -> DataFrame:
|
||||||
return mean_score(df, "Suckling")
|
return mean_score(df, "Suckling")
|
||||||
|
|
||||||
|
|
||||||
|
def fill_missing_scores(df: DataFrame) -> DataFrame:
|
||||||
|
"""
|
||||||
|
Remplacer les notes manquantes par la moyenne
|
||||||
|
des vins de la même appellation.
|
||||||
|
"""
|
||||||
|
df_copy = df.copy()
|
||||||
|
df_copy["Appellation"] = df_copy["Appellation"].astype(str).str.strip()
|
||||||
|
|
||||||
|
for score in SCORE_COLS:
|
||||||
|
df_copy[score] = to_numeric(df_copy[score], errors="coerce")
|
||||||
|
|
||||||
|
temp_cols: list[str] = []
|
||||||
|
|
||||||
|
for score in SCORE_COLS:
|
||||||
|
mean_df = mean_score(df_copy, score)
|
||||||
|
mean_name = f"mean_{score}"
|
||||||
|
temp_cols.append(mean_name)
|
||||||
|
|
||||||
|
df_copy = df_copy.merge(mean_df, on="Appellation", how="left")
|
||||||
|
df_copy[score] = df_copy[score].fillna(df_copy[mean_name])
|
||||||
|
|
||||||
|
df_copy = df_copy.drop(columns=temp_cols)
|
||||||
|
return df_copy
|
||||||
|
|||||||
14
main.py
14
main.py
@@ -9,7 +9,9 @@ from cleaning import (display_info,
|
|||||||
drop_empty_appellation,
|
drop_empty_appellation,
|
||||||
mean_robert,
|
mean_robert,
|
||||||
mean_robinson,
|
mean_robinson,
|
||||||
mean_suckling)
|
mean_suckling,
|
||||||
|
fill_missing_scores,
|
||||||
|
encode_appellation)
|
||||||
|
|
||||||
|
|
||||||
def load_csv(filename: str) -> DataFrame:
|
def load_csv(filename: str) -> DataFrame:
|
||||||
@@ -44,14 +46,18 @@ def main() -> None:
|
|||||||
|
|
||||||
robinson_means = mean_robinson(df)
|
robinson_means = mean_robinson(df)
|
||||||
save_csv(robinson_means, "mean_robinson_by_appellation.csv")
|
save_csv(robinson_means, "mean_robinson_by_appellation.csv")
|
||||||
print("\n===: moyennes Robinson par appellation ===")
|
print("\n=== moyennes Robinson par appellation ===")
|
||||||
print(robinson_means.head(10))
|
print(robinson_means.head(10))
|
||||||
|
|
||||||
suckling_means = mean_suckling(df)
|
suckling_means = mean_suckling(df)
|
||||||
save_csv(suckling_means, "mean_suckling_by_appellation.csv")
|
save_csv(suckling_means, "mean_suckling_by_appellation.csv")
|
||||||
print("\n===: moyennes Suckling par appellation ===")
|
print("\n=== moyennes Suckling par appellation ===")
|
||||||
print(suckling_means.head(10))
|
print(suckling_means.head(10))
|
||||||
|
|
||||||
|
df_missing_scores = fill_missing_scores(df)
|
||||||
|
save_csv(df_missing_scores, "donnee_filled.csv")
|
||||||
|
print("\n=== Après remplissage des notes manquantes ===")
|
||||||
|
display_info(df_missing_scores)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user