def categorize(row, target, categories_dict):
    """Helper function for adding a category column (either 'mem_category'
    or 'time_category'). Returns the category that the given row falls
    into.

    Parameters
    ----------
    row (pandas.core.series.Series): Row of dataframe.
    target (str): Either 'mem' or 'time'.
    categories_dict (dict): Dictionary of upper bounds for each category, so
        for example the dictionary {0:50, 1:100} indicates that an entry is
        category 0 if the corresponding value is <50, category 1 if the
        corresponding value is >=50 and <100.

    Returns
    ----------
        (int or None): Category of the given row.
    """
    if target == 'mem':
        val = row['used_mem']
    elif target == 'time':
        val = row['wall_time']
    else:
        raise ValueError("Target for categorization must be 'mem' or 'time'.")

    num_classes = len(categories_dict)

    if val < categories_dict[0]:
        return 0
    for i in range(1, num_classes):
        if val >= categories_dict[i-1] and val < categories_dict[i]:
            return i
    return None