Skip to content

2_Plots

prepare_for_dimensionality(df)

Factorize all categorical columns in a dataframe.

Source code in src/nhssynth/modules/dashboard/pages/2_Plots.py
def prepare_for_dimensionality(df: pd.DataFrame) -> pd.DataFrame:
    """Factorize all categorical columns in a dataframe."""
    for col in df.columns:
        if df[col].dtype == "object":
            df[col] = pd.factorize(df[col])[0]
        elif df[col].dtype == "datetime64[ns]":
            df[col] = pd.to_numeric(df[col])
        min_val = df[col].min()
        max_val = df[col].max()
        df[col] = (df[col] - min_val) / (max_val - min_val)
    return df