code: def _generate_marginal_results(
      table,
      data,
      values,
      rows,
      cols,
      aggfunc,
      observed,
      grand_margin,
      margins_name="All",
  ):
      if len(cols) > 0:
          # need to "interleave" the margins
          table_pieces = []
          margin_keys = []
  
          def _all_key(key):
              return (key, margins_name) + ("",) * (len(cols) - 1)
  
          if len(rows) > 0:
              margin = (
                  data[rows + values]
                  .groupby(rows, observed=observed)
                  .agg(aggfunc)
              )
              cat_axis = 1
  
              for key, piece in table.groupby(
                  level=0, axis=cat_axis, observed=observed
              ):
                  all_key = _all_key(key)
  
                  # we are going to mutate this, so need to copy!
                  piece = piece.copy()
                  try:
                      piece[all_key] = margin[key]
                  except TypeError:
                      # we cannot reshape, so coerce the axis
                      piece.set_axis(
                          piece._get_axis(
                              cat_axis
                          )._to_safe_for_reshape(),
                          axis=cat_axis,
                          inplace=True,
                      )
                      piece[all_key] = margin[key]
  
                  table_pieces.append(piece)
                  margin_keys.append(all_key)
          else:
              margin = grand_margin
              cat_axis = 0
              for key, piece in table.groupby(
                  level=0, axis=cat_axis, observed=observed
              ):
                  all_key = _all_key(key)
                  table_pieces.append(piece)
                  table_pieces.append(
                      Series(margin[key], index=[all_key])
                  )
                  margin_keys.append(all_key)
  
          result = concat(table_pieces, axis=cat_axis)
  
          if len(rows) == 0:
              return result
      else:
          result = table
          margin_keys = table.columns
  
      if len(cols) > 0:
          row_margin = (
              data[cols + values]
              .groupby(cols, observed=observed)
              .agg(aggfunc)
          )
          row_margin = row_margin.stack()
  
          # slight hack
          new_order = [len(cols)] + lrange(len(cols))
          row_margin.index = row_margin.index.reorder_levels(new_order)
      else:
          row_margin = Series(np.nan, index=result.columns)
  
      return result, margin_keys, row_margin
  
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 4 for calling a global function with example usage: _generate_marginal_results(table, data, values, rows, cols, aggfunc, observed, grand_margin, margins_name) and returns: result,  margin_keys,  row_margin, unknown, result