import math


def get_descriptive_statistics(df, groupby_col, target_col, suffix=""):
  """Compute the descriptive statistics (sum, std, mean) for the input
    features

  Args:
    df: dataframe for the input prediction data
    groupby_col: column name used to group the table
    target_col: column name for the descriptive statistics
    suffix: suffix for the new statistic features

  Return:
    the dataframes for the descriptive statistics
  """
  sum_col_name = f"sum_{suffix}" if len(suffix) > 0 else "sum"
  std_col_name = f"std_{suffix}" if len(suffix) > 0 else "std"
  avg_col_name = f"avg_{suffix}" if len(suffix) > 0 else "avg"

  sum = df.groupby([groupby_col])[target_col].sum().reset_index(name=sum_col_name)
  std = df.groupby([groupby_col])[target_col].std().reset_index(name=std_col_name)
  avg = df.groupby([groupby_col])[target_col].mean().reset_index(name=avg_col_name)

  # handle the special case that the std df only has one row, pd will
  # set the value to be NaN. Here we set it to be 0, which will be
  # compatible with the SVM algorithm.
  std[std_col_name] = std[std_col_name].apply(lambda x : 0 if math.isnan(x) else x)

  return sum, std, avg, [sum_col_name, std_col_name, avg_col_name]