def filter_clusters(tracks, quantile=0.8, threshold=None):
    """Filter out trajectories with a mean particle size above a given quantile.

    Parameters
    ----------
    tracks : DataFrame
        must include columns named 'particle' and 'size'
    quantile : number between 0 and 1
        quantile of particle 'size' above which to cut off
    threshold : number
        If specified, ignore quantile.

    Returns
    -------
    a subset of tracks
    """
    try:
        tracks['frame']
        tracks['particle']
    except KeyError:
        raise ValueError("Tracks must contain columns 'frame' and 'particle'.")
    if threshold is None:
        threshold = tracks['size'].quantile(quantile)

    f = lambda x: x['size'].mean() < threshold  # filtering function
    grouped = tracks.reset_index(drop=True).groupby('particle')
    filtered = grouped.filter(f)
    return filtered.set_index('frame', drop=False)