【发布时间】: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
}
在 pandas 中有没有更好的方法来做到这一点?
【问题讨论】:
标签: python-3.x pandas data-science