Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 过滤列和排序_Python_Pandas_Dataframe - Fatal编程技术网

Python 过滤列和排序

Python 过滤列和排序,python,pandas,dataframe,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

我的数据帧是-

      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')最高的模型。在这种情况下,它将打印模型名称随机森林,以创建一个布尔掩码,然后使用此掩码获得列
中最大值的
索引,其中度量为
AUC
,最后使用此索引获得相应的
模型

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

print(model)

'Random_Forest'
给你:

df.loc[df.Metric=='AUC',['Value','Model']].max()['Model']
##--结束粘贴的文本--
Out[1]:“随机森林”

< /代码> 可选地,您也可以考虑通过< <代码> >代码> >所有代码> >代码> >度量> <代码>行:

df.groupby(['Metric'], as_index=False)['Value','Model'].max()
您还可以为“AUC”度量的“模型”列
.query()

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

如果您想使用基本功能,那么:

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'  

您的问题是否旨在筛选和重新排序
df[df.Metric==“AUC”]。排序值(by=“Value”,升序=False)[“Model”]。[0]
这就像为您的需求编写代码一样简单