def apply_to_feature(feature_df,groupby_func_name=None,function=None):
    """
    Apply a function to the entries for each feature.
    feature_df ... dataframe with index (chrom, feature_name, pos)
                   (Such as the output of data_per_feature())
    groupby_func_name ... name of the function of the groupby object
                         to apply to the data
                          This is faster than applying a function object.
    function ... alternatively: function object to apply
    """
    groups = feature_df.groupby(lambda idx: idx[1])
    if groupby_func_name is not None:
        return getattr(groups,groupby_func_name)()
    elif function is not None:
        return groups.apply(function)
    else:
        raise ValueError("Either groupby_func_name or function have to be given.")