code: def sample_groups(df, by, n_samples):
      # Sample n_samples in the df using the by column
  
      # Step 1
      # first, shuffle the dataframe and reset its index,
      # so that the ordering of values within each group
      # is made random:
      df = df.sample(frac=1).reset_index(drop=True)
  
      # If we want to keep all samples we return
      if n_samples == -1:
          return df
      # Step 2
      # add an integer-encoded version of the "by" column,
      # since the rank aggregation seems not to work for
      # non-numeric data
      df["_"] = df[by].astype("category").cat.codes
  
      # Step 3
      # now do a "rank" aggregation and filter out only
      # the first N_SAMPLES ranks.
      result = df.loc[df.groupby(by)["_"].rank("first") <= n_samples, :]
      del result["_"]
      return result
  
imports: import importlib
  import os
  import shutil
  from numba import cuda
  import cudf
  from cudf.core.column import as_column
  from cuda.cudart import cudaDeviceAttr
  from rmm._cuda.gpu import getDeviceAttribute
label_desc: Use image node_id 16 for calling a global function with example usage: sample_groups(df, by, n_samples) and returns: result, df