code: def encode_target_(
      tbl,
      targets,
      target_cols=None,
      drop_cat=True,
      drop_fold=True,
      fold_col=None,
  ):
      for target_code in targets:
          cat_col = target_code.cat_col
          out_target_mean = target_code.out_target_mean
  
          join_tbl = tbl._clone(target_code.df)
  
          # (keys of out_target_mean) should include (output columns)
          output_columns = list(
              filter(
                  lambda x: (
                      (isinstance(cat_col, str) and x != cat_col)
                      or (
                          isinstance(cat_col, list) and x not in cat_col
                      )
                  )
                  and (fold_col is not None and x != fold_col),
                  join_tbl.df.columns,
              )
          )
          for column in output_columns:
              assert column in out_target_mean, (
                  column + " should be in out_target_mean"
              )
              column_mean = out_target_mean[column][1]
              assert isinstance(column_mean, int) or isinstance(
                  column_mean, float
              ), (
                  "mean in target_mean should be numeric but get {} of type {}"
                  " in {}".format(
                      column_mean, type(column_mean), out_target_mean
                  )
              )
  
          # select target_cols to join
          if target_cols is not None:
              new_out_target_mean = {}
              for out_col, target_mean in out_target_mean.items():
                  if target_mean[0] not in target_cols:
                      join_tbl = join_tbl.drop(out_col)
                  else:
                      new_out_target_mean[out_col] = target_mean
              out_target_mean = new_out_target_mean
  
          if fold_col is None:
              tbl = tbl.join(join_tbl, on=cat_col, how="left")
          else:
              if isinstance(cat_col, str):
                  tbl = tbl.join(
                      join_tbl, on=[cat_col, fold_col], how="left"
                  )
              else:
                  tbl = tbl.join(
                      join_tbl, on=cat_col + [fold_col], how="left"
                  )
  
          # for new columns, fill na with mean
          for out_col, target_mean in out_target_mean.items():
              if out_col in tbl.df.columns:
                  tbl = tbl.fillna(target_mean[1], out_col)
  
      if drop_cat:
          for target_code in targets:
              if isinstance(target_code.cat_col, str):
                  tbl = tbl.drop(target_code.cat_col)
              else:
                  tbl = tbl.drop(*target_code.cat_col)
  
      if drop_fold:
          if fold_col is not None:
              tbl = tbl.drop(fold_col)
  
      return tbl
  
imports: from zoo.common.utils import callZooFunc
  from pyspark.sql.types import IntegerType, ShortType, LongType, FloatType, DecimalType, DoubleType
label_desc: Use image node_id 23 for calling a global function with example usage: encode_target_(tbl, targets, target_cols, drop_cat, drop_fold, fold_col) and returns: tbl