def bin_frequencies(df, how='max', bin_size=5, n_bins=None):
    """
    bins spectral data frequencies to the specified bin size or number of bins
    :param df: dataframe of spectral data from single sensor
    :param how: how to aggregate the intensities for each bins. Any numpy aggregate function, default is max
    :param bin_size: size of frequency bins, default is 5. Overriden by n_bins if specified
    :param n_bins: number of bins of equal size to return. Overrides bin_size. Default is None
    :return: dataframe with same number or rows but reduced number of columns
    """
    df = df.T.reset_index()
    df['index'] = df['index'].astype('float')
    if n_bins:
        f_min = df['index'].min()
        f_max = df['index'].max()
        bin_size = (f_max - f_min) // n_bins
        df['freq_bin'] = (df['index'] // bin_size) * bin_size
    else:
        df['freq_bin'] = (df['index'] // bin_size) * bin_size
    df = df.groupby('freq_bin').agg(how).drop('index', axis=1).T
    return df