【问题标题】:aggregation and indexing based on specific column in pandas基于 pandas 中特定列的聚合和索引
【发布时间】:2020-05-26 18:40:22
【问题描述】:

我有一个按国家/地区与世界幸福数据相关的 csv 文件。在该数据文件中,与幸福相关的不同分数是根据某些特定标准计算的。我想根据这些标准(特征)找到最差和最好的国家。下面用笔记本图片给出了我的解决方案:

happiness_df = pd.read_csv('Datasets/happiness_2017.csv')
happiness_data_by_country = {}
for column in happiness_df.describe().columns:
    if column != 'Rank':       
        max_val = happiness_df.describe().loc['max',column]
        min_val = happiness_df.describe().loc['min',column]
        country_with_max = happiness_df.loc[happiness_df[column]==max_val,'Country'].values[0]
        country_with_min = happiness_df.loc[happiness_df[column]==min_val,'Country'].values[0]
        happiness_data_by_country[column] = {
        "worst" : country_with_min,
        "best" : country_with_max 
    }

dataframe

my solution

在 pandas 中有没有更好的方法来做到这一点?

【问题讨论】:

    标签: python-3.x pandas data-science


    【解决方案1】:

    是的,你可以尝试最大值:

    df.loc[df['HappinessScore'].idxmax()]

    至少:df.loc[df['HappinessScore'].idxmin()]。

    【讨论】:

    • 谢谢,我不知道 idxmin,idxmax 函数。
    猜你喜欢
    • 2016-11-22
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 2020-09-28
    相关资源
    最近更新 更多