在Python中的.CSV文件中查找max number

2024-09-28 19:07:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个.csv文件,在Excel中打开时如下所示: enter image description here

我的代码:

myfile = open("/Users/it/Desktop/Python/In-Class Programs/countries.csv", "rb")

    countries = []
    for item in myfile:
        a = item.split(",")
        countries.append(a)

    hdi_list = []
    for acountry in countries:
        hdi = acountry[3]

        try:
            hdi_list.append(float(hdi))
        except:
            pass

    average = round(sum(hdi_list)/len(hdi_list), 2)
    maxNumber = round(max(hdi_list), 2)
    minNumber = round(min(hdi_list), 2)

这个代码工作得很好,但是,当我找到max、min或avg时,我需要获取相应的国家名称并将其打印出来。在

如何更改我的代码以获取最小值、最大值、平均值的国家名称?在


Tags: csv代码infor国家minitemmyfile
3条回答

下面的方法与您的实现非常接近,我认为它可能有用。但是,如果您开始处理更大或更复杂的csv文件,您应该查看类似于csv.reader或“熊猫”(如前所述)。它们在处理复杂的.csv数据时更加健壮和高效。你也可以使用“xlrd”包在Excel中工作。在

在我看来,引用国家名称和它们各自的值的最简单的解决方案是组合您的“for循环”。不要在数据中循环两次(在两个单独的“for循环”中)和创建两个单独的列表,而是使用一个“for循环”并创建一个包含相关数据的字典(即“country name”、“hdi”)。您也可以创建元组(如前所述),但我认为字典更显式。在

myfile = open("/Users/it/Desktop/Python/In-Class Programs/countries.csv", "rb")

countries = []
for line in myfile:
    country_name = line.split(",")[1]
    value_of_interest = float(line.split(",")[3])
    countries.append(
        {"Country Name": country_name, 
         "Value of Interest": value_of_interest})

ave_value = sum([country["Value of Interest"] for country in countries])/len(countries)
max_value = max([country["Value of Interest"] for country in countries])
min_value = min([country["Value of Interest"] for country in countries])

print "Country Average == ", ave_value
for country in countries:
    if country["Value of Interest"] == max_value:
        print "Max == {country}:{value}".format(country["Country Name"], country["Value of Interest"])
    if country["Value of Interest"] == min_value:
        print "Min == {country}:{value}".format(country["Country Name"], country["Value of Interest"])

请注意,如果多个国家的最小值/最大值相等,则此方法将返回多个国家。在

如果您不想创建单独的列表(比如您当前的实现),可以考虑使用zip()连接列表(按索引),其中

^{pr2}$

例如:

for country in zip(countries, hdi_list):
    if country[1] == max_value:
        print country[0], country[1]

把类似的逻辑应用到最小值和平均值。这种方法有效,但不太明确,更难维护。在

使用下面的pandas模块,[4][5],和{}应该分别显示最大值、最小值和平均值。请注意,下面的数据与您的save for country不匹配。在

In [1]: import pandas as pd

In [2]: df = pd.read_csv("hdi.csv")

In [3]: df
Out[3]: 
         Country    HDI
0         Norway  83.27
1      Australia  80.77
2    Netherlands  87.00
3  United States  87.43
4    New Zealand  87.43
5         Canada  87.66
6        Ireland  75.47
7  Liechtenstein  88.97
8        Germany  86.31
9         Sweden  80.54

In [4]: df.ix[df["HDI"].idxmax()]
Out[4]: 
Country    Liechtenstein
HDI                88.97
Name: 7, dtype: object

In [5]: df.ix[df["HDI"].idxmin()]
Out[5]: 
Country    Ireland
HDI          75.47
Name: 6, dtype: object

In [6]: df["HDI"].mean()
Out[6]: 84.484999999999985

假设LiechtensteinGermany都有最大值:

^{pr2}$

同样的逻辑也适用于最小值。在

不要直接将值放入列表中,而是使用元组,如下所示:

hdi_list.append((float(hdi), acountry[1]))

然后您可以使用此选项:

^{pr2}$

相关问题 更多 >