• pandas数组获取最大值索引的方法-argmax和idxmax


     pandas Series 的 argmax 方法和 idxmax 方法用于获取 Series 的最大值的索引值:

    举个栗子:

    有一个pandas Series,它的索引是国家名,数据是就业率,要找出就业率最高的国家:

    import pandas as pd
    
    countries = [
        'Afghanistan', 'Albania', 'Algeria', 'Angola',
        'Argentina', 'Armenia', 'Australia', 'Austria',
        'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh',
        'Barbados', 'Belarus', 'Belgium', 'Belize',
        'Benin', 'Bhutan', 'Bolivia', 'Bosnia and Herzegovina',
    ]
    
    
    employment_values = [
        55.70000076,  51.40000153,  50.5       ,  75.69999695,
        58.40000153,  40.09999847,  61.5       ,  57.09999847,
        60.90000153,  66.59999847,  60.40000153,  68.09999847,
        66.90000153,  53.40000153,  48.59999847,  56.79999924,
        71.59999847,  58.40000153,  70.40000153,  41.20000076,
    ]
    
    # Employment data in 2007 for 20 countries
    employment = pd.Series(employment_values, index=countries)

    可以这样做:

    max_country = employment.idxmax()     
    
    max_country = employment.argxmax()  

    # 结果: 'Angola'

    如果是一个没有索引值的Series,则返回它的位置索引:

    pure_employment = pd.Series(employment_values)
    print(pure_employment.argmax())
    print(pure_employment.idxmax())
    
    # 结果: 3
  • 相关阅读:
    SolrCloud阶段总结
    Solr总结
    机器学习算法与Python实践之(六)二分k均值聚类
    机器学习问题方法总结
    浅谈Kmeans聚类
    AVL树的算法思路整理
    Solr4.6从数据库导数据的步骤
    红黑树
    浅谈 Adaboost 算法
    POJ 1836 Alignment
  • 原文地址:https://www.cnblogs.com/liulangmao/p/9211537.html
Copyright © 2020-2023  润新知