def filter_sequences(df, lower_bound, upper_bound, grouping_col='hadm_id'):
    """
    Filters sequences so that only those that are between lower_bound and upper_bound remain
    Parameters
    ----------
    df: object
        data on which to operate on
    lower_bound: int
        lower bound to discard
    upper_bound: int
        upper bound to truncate on
    grouping_col: str
        column to group by

    Returns
    -------
    sequences that are in between lower_bound and upper_bound
    """
    print("Start filtering procedure")
    df_grouped = df.groupby(grouping_col)
    if df_grouped.size().max() > upper_bound:
        df = df_grouped.apply(lambda group: group[:upper_bound]).reset_index(drop=True)
        print('Finished grouping')
    del df_grouped
    if lower_bound >= 1:
        df = df.groupby(grouping_col).filter(lambda group: len(group) > lower_bound).reset_index(drop=True)
        print('Finished filtering')
    return df