code: def _normalize(table, normalize, margins, margins_name="All"):
      if not isinstance(normalize, bool) and not isinstance(
          normalize, compat.string_types
      ):
          axis_subs = {0: "index", 1: "columns"}
          try:
              normalize = axis_subs[normalize]
          except KeyError:
              raise ValueError("Not a valid normalize argument")
  
      if margins is False:
          # Actual Normalizations
          normalizers = {
              "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:
              raise ValueError("Not a valid normalize argument")
  
          table = f(table)
          table = table.fillna(0)
  
      elif margins is True:
          column_margin = table.loc[:, margins_name].drop(margins_name)
          index_margin = table.loc[margins_name, :].drop(margins_name)
          table = table.drop(margins_name, axis=1).drop(margins_name)
          # to keep index and columns names
          table_index_names = table.index.names
          table_columns_names = table.columns.names
  
          # 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)
  
          elif normalize == "index":
              index_margin = index_margin / index_margin.sum()
              table = table.append(index_margin)
              table = table.fillna(0)
  
          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)
  
          else:
              raise ValueError("Not a valid normalize argument")
  
          table.index.names = table_index_names
          table.columns.names = table_columns_names
  
      else:
          raise ValueError("Not a valid margins argument")
  
      return table
  
imports: import numpy
  from pandas.compat import lrange, range, zip
  from pandas.util._decorators import Appender, Substitution
  from pandas.core.dtypes.cast import maybe_downcast_to_dtype
  from pandas.core.dtypes.common import is_integer_dtype, is_list_like, is_scalar
  from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
  from pandas import compat
  import pandas.core.common
  from pandas.core.frame import _shared_docs
  from pandas.core.groupby import Grouper
  from pandas.core.index 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 9 for calling a global function with example usage: _normalize(table, normalize, margins, margins_name) and returns: table