[Python] Add a new row as the average of columns

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    Give the following dataframe:

    _BETTER _SAME _WORSE ___dataset Metric
    0.373802 0.816794 0.568783 Train precision
    0.391304 0.865229 0.519324 Train recall
    0.382353 0.840314 0.542929 Train f1-score
    0.500000 1.000000 0.583333 Val precision
    0.333333 1.000000 0.736842 Val recall
    0.400000 1.000000 0.651163 Val f1-score
    0.000000 0.000000 0.666667 Test precision
    0.000000 0.000000 0.500000 Test recall
    0.000000 0.000000 0.571429 Test f1-score


    would like to add the followings:

    _BETTER _SAME _WORSE ___dataset Metric
    0.373802 0.816794 0.568783 Train precision
    0.391304 0.865229 0.519324 Train recall
    0.382353 0.840314 0.542929 Train f1-score
    0.500000 1.000000 0.583333 Val precision
    0.333333 1.000000 0.736842 Val recall
    0.400000 1.000000 0.651163 Val f1-score
    0.000000 0.000000 0.666667 Test precision
    0.000000 0.000000 0.500000 Test recall
    0.000000 0.000000 0.571429 Test f1-score
    mean_p_b mean_p_s mean_p_w All precision_avg
    mean_r_b mean_r_s mean_r_w All recall_avg
    mean_f1_b mean_f1_s mean_f1_w All f1_score_avg


    where mean_p_b mean_p_s mean_p_w is obtained by the average of the precision row, w.r.t the three colums, respectively. Likewise the mean_r_b mean_r_s mean_r_w and mean_f1_b mean_f1_s mean_f1_w.

    Applying each separately:

    df_avg_precision["BETTER"] = (df_train_precision['_BETTER'].values + df_val_precision['_BETTER'].values +
    df_test_precision['_BETTER'].values)/3
    df_avg_precision["Metric"] = "precision_avg"
    df_avg_recall["BETTER"] = (df_train_recall['_BETTER'].values +
    df_val_recall['_BETTER'].values + df_test_recall['_BETTER'].values)/3
    df_avg_recall["Metric"] = "recall_avg"
    df_avg_f1["BETTER"] = (df_train_f1['_BETTER'].values +
    df_val_f1['_BETTER'].values + df_test_f1['_BETTER'].values)/3
    df_avg_f1["Metric"] = "f1_avg"`

    Continue reading...

Compartilhe esta Página