def filter_optimized_results(df, err_param_name, score_name, is_higher_score_better):
    """Removes suboptimal rows from the dataframe, returning only the best ones.

    Args:
        df (pandas.DataFrame): A dataframe containing all the data returned by the runner.
        err_param_name (str): The name of the error parameter by which the data will be grouped.
        score_name (str): The name of the score type we want to optimize.
        is_higher_score_better (bool): If true, then only the highest results are returned.
            Otherwise the lowest results are returned.

    Returns:
        pandas.DataFrame: A dataframe containing the optimized results.
    """
    if is_higher_score_better:
        df_ = df.loc[df.groupby(err_param_name, sort=False)[score_name].idxmax()].reset_index(drop=True)
    else:
        df_ = df.loc[df.groupby(err_param_name, sort=False)[score_name].idxmin()].reset_index(drop=True)
    df_.name = df.name
    return df_