def filter_contacts(cf, source_sel, target_sel, filter_sel=('target',
    'distance')):
    """
    Filter contact dataframe for multiple occurrences of source-target
    selections based on a filter criterion.

    :param cf:          Contact DataFrame
    :type cf:           :pandas:DataFrame
    :param source_sel:  Column label for source selection
    :type source_sel:   :py:str
    :param target_sel:  Column label for target selection
    :type target_sel:   :py:sel
    :param filter_sel:  Column label to sort selection on
                        and filter all but the first row

    :return:            Filtered contact DataFrame
    :rtype:             :pandas:DataFrame
    """
    exclude_index = []
    for pair in (('source', 'target'), ('target', 'source')):
        source_mult = cf[pair[0], source_sel].value_counts()
        source_mult = set(source_mult[source_mult > 1].index)
        target_mult = cf[pair[1], target_sel].value_counts()
        target_mult = set(target_mult[target_mult > 1].index)
        for a in source_mult:
            for b in target_mult:
                sel = cf[(cf[pair[0], source_sel] == a) & (cf[pair[1],
                    target_sel] == b)]
                if len(sel) > 1:
                    exclude_index.extend(list(sel.sort_values(by=filter_sel
                        ).iloc[1:].index))
    return cf.loc[~cf.index.isin(exclude_index)]
