code: def _normalize(
      table: DataFrame,
      normalize,
      margins: bool,
      margins_name: Hashable = "All",
  ) -> DataFrame:
      if not isinstance(normalize, (bool, str)):
          axis_subs = {0: "index", 1: "columns"}
          try:
              normalize = axis_subs[normalize]
          except KeyError as err:
              raise ValueError(
                  "Not a valid normalize argument"
              ) from err
  
      if margins is False:
          # Actual Normalizations
          normalizers: dict[bool | str, Callable] = {
              "all": lambda x: x / x.sum(axis=1).sum(axis=0),
              "columns": lambda x: x / x.sum(),
              "index": lambda x: x.div(x.sum(axis=1), axis=0),
          }
  
          normalizers[True] = normalizers["all"]
  
          try:
              f = normalizers[normalize]
          except KeyError as err:
              raise ValueError(
                  "Not a valid normalize argument"
              ) from err
  
          table = f(table)
          table = table.fillna(0)
  
      elif margins is True:
          # keep index and column of pivoted table
          table_index = table.index
          table_columns = table.columns
          last_ind_or_col = table.iloc[-1, :].name
  
          # check if margin name is not in (for MI cases) and not equal to last
          # index/column and save the column and index margin
          if (margins_name not in last_ind_or_col) & (
              margins_name != last_ind_or_col
          ):
              raise ValueError(
                  f"{margins_name} not in pivoted DataFrame"
              )
          column_margin = table.iloc[:-1, -1]
          index_margin = table.iloc[-1, :-1]
  
          # keep the core table
          table = table.iloc[:-1, :-1]
  
          # Normalize core
          table = _normalize(table, normalize=normalize, margins=False)
  
          # Fix Margins
          if normalize == "columns":
              column_margin = column_margin / column_margin.sum()
              table = concat([table, column_margin], axis=1)
              table = table.fillna(0)
              table.columns = table_columns
  
          elif normalize == "index":
              index_margin = index_margin / index_margin.sum()
              table = table._append(index_margin)
              table = table.fillna(0)
              table.index = table_index
  
          elif normalize == "all" or normalize is True:
              column_margin = column_margin / column_margin.sum()
              index_margin = index_margin / index_margin.sum()
              index_margin.loc[margins_name] = 1
              table = concat([table, column_margin], axis=1)
              table = table._append(index_margin)
  
              table = table.fillna(0)
              table.index = table_index
              table.columns = table_columns
  
          else:
              raise ValueError("Not a valid normalize argument")
  
      else:
          raise ValueError("Not a valid margins argument")
  
      return table
  
imports: from __future__ import annotations
  from collections.abc import Hashable, Sequence
  from typing import TYPE_CHECKING, Callable, Literal, cast
  import warnings
  import numpy
  from pandas._libs import lib
  from pandas.util._decorators import Appender, Substitution
  from pandas.util._exceptions import find_stack_level
  from pandas.core.dtypes.cast import maybe_downcast_to_dtype
  from pandas.core.dtypes.common import is_list_like, is_nested_list_like, is_scalar
  from pandas.core.dtypes.dtypes import ExtensionDtype
  from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
  import pandas.core.common
  from pandas.core.frame import _shared_docs
  from pandas.core.groupby import Grouper
  from pandas.core.indexes.api import Index, MultiIndex, get_objs_combined_axis
  from pandas.core.reshape.concat import concat
  from pandas.core.reshape.util import cartesian_product
  from pandas.core.series import Series
label_desc: Use image node_id 10 for calling a global function with example usage: _normalize(table, normalize, margins, margins_name) and returns: table