def baseline_subtract(df, col, ref, window):
    """
    This function will baseline subtract a specified column of the dataframe based on a provided window within a reference column.

    Arguments
    -------------------
    df: dataframe
        A pandas dataframe with columns ``col`` and ``ref``.
    col: string
        A string indicating the name of the column to be baseline subtracted.
    ref: string
        A reference column over-which to find the window.
    window: list
        An iterable with the start and end coordinates of the reference window for baseline subtraction.
    
    Returns
    ------------------
    dataframe
        A modified pandas dataframe where the the column ``col`` has been baseline subtracted.
    """

    iblsub = []
    grouped = df.groupby('sweep')
    baselines = df.query(f'{ref} >= @window[0] and {ref} < @window[1]').groupby('sweep')[col].mean()
                
    for name,group in grouped[col]:
        iblsub.append(group-baselines[name])
        
    flatList = [item for sublist in iblsub for item in sublist]
    
    return(df)