code: def rank_categorical(
      df: pd.DataFrame,
      columns_to_rank: List[str],
      replace_unseen: Union[str, float] = nan,
      store_mapping: bool = False,
  ) -> LearnerReturnType:
      """
      Rank categorical features by their frequency in the train set.
  
      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_rank : list of str
          The df columns names to perform the rank.
  
      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
      """
  
      def col_categ_getter(col: str) -> Dict:
          return (
              df[col]
              .value_counts()
              .reset_index()
              .sort_values([col, "count"], ascending=[True, False])
              .set_index(col)["count"]
              .rank(method="first", ascending=False)
              .to_dict()
          )
  
      vec = {
          column: col_categ_getter(column) for column in columns_to_rank
      }
  
      def p(new_df: pd.DataFrame) -> pd.DataFrame:
          return apply_replacements(
              new_df, columns_to_rank, vec, replace_unseen
          )
  
      p.__doc__ = learner_pred_fn_docstring("rank_categorical")
  
      log: LearnerLogType = {
          "rank_categorical": {
              "transformed_column": columns_to_rank,
              "replace_unseen": replace_unseen,
          }
      }
  
      if store_mapping:
          log["rank_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 10 for calling a global function with example usage: rank_categorical(df, columns_to_rank, replace_unseen, store_mapping) and returns: p,  p,  log, df, apply_replacements