筛选列和排名熊猫

时间:2020-07-04 07:50:22

标签: python pandas dataframe

我的数据框是-

      Metric       Value              Model
0   Accuracy        87.608760       Logistic_Regression
1   Neg_log_loss    -0.332951       Logistic_Regression
2   F1_measure      0.854182        Logistic_Regression
3   AUC             0.927378        Logistic_Regression
4   Precision       0.871396        Logistic_Regression
5   Recall          0.837687        Logistic_Regression
6   Accuracy        96.433245       Random_Forest
7   Neg_log_loss   -0.105780        Random_Forest
8   F1_measure      0.958133        Random_Forest
9   AUC             0.994008        Random_Forest
10  Precision       0.974733        Random_Forest
11  Recall          0.942097        Random_Forest
12  Accuracy        84.836008       Naive_Bayes
13  Neg_log_loss   -0.917701        Naive_Bayes
14  F1_measure      0.823289        Naive_Bayes
15  AUC             0.915744        Naive_Bayes
16  Precision       0.831528        Naive_Bayes
17  Recall          0.815300        Naive_Bayes

metric ='AUC'

现在,我要选择“度量”列('AUC')最高的模型。在这种情况下,它将打印model_name Random_Forest

4 个答案:

答案 0 :(得分:2)

使用Series.eq创建一个布尔掩码,然后将该掩码与Series.idxmax一起使用以获取度量标准所在的列index中最大值的Value AUC,最后使用此索引来获取相应的Model

ind =df.loc[df['Metric'].eq('AUC'), 'Value'].idxmax()
model = df.loc[ind, 'Model']

结果:

print(model)

'Random_Forest'

答案 1 :(得分:0)

您在这里:

df.loc[df.Metric == 'AUC', ['Value', 'Model']].max()['Model']

## -- End pasted text --
Out[1]: 'Random_Forest'

答案 2 :(得分:0)

除了其他答案,您还可以考虑将df按所有max()行的'Metric'分组:

df.groupby(['Metric'], as_index=False)['Value','Model'].max()

然后您还可以.query()进入“ AUC”指标的“模型”列:

df.groupby(['Metric'], as_index=False)['Value','Model'].max().query('Metric == "AUC"')['Model']

答案 3 :(得分:0)

如果您想使用基础知识,那么:

empty_value_list=[]

for i,j in zip(df['Metric'],df['Value']):
    if i=='AUC':
        empty_value_list.append(j)

max_value=max(empty_value_list)

for i,j,k in zip(df['Metric'],df['Value'],df['Model'])
    if i=='AUC' and j==max_value:
        print(k)
      
Out[1]: 'Random_Forest'