Getting index name for the max value in DF [duplicate]

↘锁芯ラ 提交于 2021-02-16 15:09:26

问题


I have the following dataframe:

data = {'Algorithm': ['KNN', 'Decision Tree', 'SVM', 'Logistic Regression'], 
        'Jaccard': [0.75,0.65,0.67,0.70], 
        'F1-score': [0.69,0.78, 0.75, 0.77], 
        'LogLoss': ['NA', 'NA', 'NA', 5.23]}
report= pd.DataFrame(data = data)
report = report[['Algorithm', 'Jaccard', 'F1-score', 'LogLoss']]
report.set_index(report['Algorithm'], inplace = True)
report.drop(columns = ['Algorithm'], inplace = True)

What i want to do is to print out the name of the index with the highest value in the dafaframe. It would be something like this:

print(report['Jaccard'][index_name_with_highest_value])

it should yield:

'KNN'

Thank you in advance :)


回答1:


Try np.where:

print(report.index[np.where(report['Jaccard'].max())[0][0]])

Updated Try np.where:

print(report['Algorithm'][np.where(report['Jaccard'].max())[0][0]])

Or idxmax:

print(report['Jaccard'].idxmax())

Update:

print(report['Algorithm'][np.where(report['Jaccard']==report['Jaccard'].max())[0][0]])

@jezrael's solution is also very good:

print(report.index[report['Jaccard'] == report['Jaccard'].max()])


来源:https://stackoverflow.com/questions/52420881/getting-index-name-for-the-max-value-in-df

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!