code: def _generate_marginal_results(
      table,
      data,
      values,
      rows,
      cols,
      aggfunc,
      observed,
      margins_name: Hashable = "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()
                  piece[all_key] = margin[key]
  
                  table_pieces.append(piece)
                  margin_keys.append(all_key)
          else:
              from pandas import DataFrame
  
              cat_axis = 0
              for key, piece in table.groupby(
                  level=0, axis=cat_axis, observed=observed
              ):
                  if len(cols) > 1:
                      all_key = _all_key(key)
                  else:
                      all_key = margins_name
                  table_pieces.append(piece)
                  # GH31016 this is to calculate margin for each group, and assign
                  # corresponded key as index
                  transformed_piece = DataFrame(piece.apply(aggfunc)).T
                  if isinstance(piece.index, MultiIndex):
                      # We are adding an empty level
                      transformed_piece.index = MultiIndex.from_tuples(
                          [all_key], names=piece.index.names + [None]
                      )
                  else:
                      transformed_piece.index = Index(
                          [all_key], name=piece.index.name
                      )
  
                  # append piece for margin into table_piece
                  table_pieces.append(transformed_piece)
                  margin_keys.append(all_key)
  
          if not table_pieces:
              # GH 49240
              return table
          else:
              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)] + list(range(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: from __future__ import annotations
  from typing import TYPE_CHECKING, Callable, Hashable, Sequence, cast
  import numpy
  from pandas._libs import lib
  from pandas._typing import AggFuncType, AggFuncTypeBase, AggFuncTypeDict, IndexLabel
  from pandas.util._decorators import Appender, Substitution
  from pandas.core.dtypes.cast import maybe_downcast_to_dtype
  from pandas.core.dtypes.common import is_extension_array_dtype, is_integer_dtype, is_list_like, is_nested_list_like, is_scalar
  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 5 for calling a global function with example usage: _generate_marginal_results(table, data, values, rows, cols, aggfunc, observed, margins_name) and returns: result,  margin_keys,  row_margin, unknown, table, result