【问题标题】:How to print name under the column that has the max value? (pandas) (python)如何在具有最大值的列下打印名称? (熊猫)(蟒蛇)
【发布时间】:2022-11-02 03:20:24
【问题描述】:

我正在使用的数据表按以下列组织:“国家”、“互联网用户”和“人口”。我能够计算并打印使用互联网的最大人口百分比,但是如何打印具有该最大百分比的国家/地区名称。例如,印度尼西亚的最大值为 94%,但我只能打印“94%”。我想打印“印度尼西亚 94%”。

import pandas as pd
import matplotlib.pyplot as plt

pop = pd.read_csv('country_internet.csv')
op = input("Enter output file name: ")

pop['Percentage'] = round(pop['Internet users']/pop['Population']*100,2)
pop.plot(x = 'Country', y = 'Percentage')

print("Maximum percentage of all countries:",pop['Percentage'].max(),"%")

plt.show()
fig = plt.gcf()
fig.savefig(op)

【问题讨论】:

  • 你可以试试row = pop.iloc[pop['Percentage'].idxmax()] print(row['Country']) 等。

标签: python pandas


【解决方案1】:

这应该这样做:

print('Country with max percentage is: {}'.format(df.loc[df['Percentage'] == df['Percentage'].max(), 'Country'])

【讨论】:

  • 看起来不错!他们确实想要国家和百分比,所以也许编辑你的答案以添加百分比。当然,也使用:.0% 格式化。 :)
猜你喜欢
  • 2016-10-16
  • 2021-11-09
  • 1970-01-01
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
  • 2022-01-09
相关资源
最近更新 更多