def get_grouped_metric(df, group_column, metric_column, operation):
    """Group by a specified column, then perform a mathematical operation on a given
    metric column and return the value so it can be assigned to the DataFrame.
    For example, group by ID and sum the value of orders placed by the ID.

    :param df: Pandas DataFrame
    :param group_column: Column name to use for groupby, i.e. id
    :param metric_column: Column name for metric column, i.e. visits
    :param operation: Operation to perform  (sum, count, nunique, min, max, median, mean)
    """

    return df.groupby(group_column)[metric_column].transform(operation)