def filt_get_last_index(data, target, 
                        idx_col=['DESY_SORT_KEY', 'INDEX_CLAIM_ORDER'],
                        min_occurence=4
                       ):
    """Filter to get last index claim for each patient.

    Filters are designed to be composible functions such that one can chain filters. 
    Outputs 
    filtered `data` and `target` with entries for only the last index claim

    Parameters
    ----------
    data : DataFrame
        feature data
    target : DataFrame 
        target data
    idx_col: str or int or List of str|int
        index columns
    min_occurence: int
        number of minimum occurence required for an instance to be included

    Returns
    -------
    data: DataFrame
    target: DataFrame
    """
    # last index claim for each patient
    last_claim_idx = (data.reset_index()[idx_col].groupby([idx_col[0]])   # Group by pateint id
                          .max()[idx_col[1]].to_frame()                   # take last index claim order for a patient
                          .reset_index().set_index(idx_col))              # set index to patient id and index_claim_order

    # filter data and keep only last index claim for each patient and its history
    data = data[data.reset_index().set_index(idx_col).index.isin(last_claim_idx.index)]
    
    # remove all patients (last claim index) who have only one claim as it is not useful for med2vec  
    temp = data.reset_index().groupby(idx_col).count().iloc[:,0]
   
    useful_claims_idx = temp[temp>=min_occurence].index 

    data = data[data.index.isin(useful_claims_idx)]
    if target is not None:
        target = target[target.index.isin(data.index)]

    return data, target