code: def truncate_categorical(
      df: pd.DataFrame,
      columns_to_truncate: List[str],
      percentile: float,
      replacement: Union[str, float] = -9999,
      replace_unseen: Union[str, float] = -9999,
      store_mapping: bool = False,
  ) -> LearnerReturnType:
      """
      Truncate infrequent categories and replace them by a single one.
      You can think of it like "others" category.
  
      The default behaviour is to replace the original values. To store
      the original values in a new column, specify `prefix` or `suffix`
      in the parameters, or specify a dictionary with the desired column
      mapping using the `columns_mapping` parameter.
  
      Parameters
      ----------
      df : pandas.DataFrame
          A Pandas' DataFrame that must contain a `prediction_column` columns.
  
      columns_to_truncate : list of str
          The df columns names to perform the truncation.
  
      percentile : float
          Categories less frequent than the percentile will be replaced by the
          same one.
  
      replacement: int, str, float or nan
          The value to use when a category is less frequent that the percentile
          variable.
  
      replace_unseen : int, str, float, or nan
          The value to impute unseen categories.
  
      store_mapping : bool (default: False)
          Whether to store the feature value -> integer dictionary in the log.
      """
      get_categs = lambda col: (
          df[col].value_counts() / len(df)
      ).to_dict()
      update = lambda d: map(
          lambda kv: (kv[0], replacement)
          if kv[1] <= percentile
          else (kv[0], kv[0]),
          d.items(),
      )
      categs_to_dict = lambda categ_dict: dict(categ_dict)
  
      vec = {
          column: compose(categs_to_dict, update, get_categs)(column)
          for column in columns_to_truncate
      }
  
      def p(new_df: pd.DataFrame) -> pd.DataFrame:
          return apply_replacements(
              new_df, columns_to_truncate, vec, replace_unseen
          )
  
      p.__doc__ = learner_pred_fn_docstring("truncate_categorical")
  
      log: LearnerLogType = {
          "truncate_categorical": {
              "transformed_column": columns_to_truncate,
              "replace_unseen": replace_unseen,
          }
      }
  
      if store_mapping:
          log["truncate_categorical"]["mapping"] = vec
  
      return p, p(df), log
  
imports: from typing import Any, Callable, Dict, List, Union, Optional
  import numpy
  import pandas
  from numpy import nan
  from sklearn.preprocessing import StandardScaler
  from statsmodels.distributions import empirical_distribution
  from toolz import curry, merge, compose, mapcat
  from fklearn.common_docstrings import learner_return_docstring, learner_pred_fn_docstring
  from fklearn.training.utils import log_learner_time
  from fklearn.types import LearnerReturnType, LearnerLogType
  from fklearn.preprocessing.schema import column_duplicatable
label_desc: Use image node_id 9 for calling a global function with example usage: truncate_categorical(df, columns_to_truncate, percentile, replacement, replace_unseen, store_mapping) and returns: p,  p,  log, apply_replacements