Python3:在csv列中找到最大值并打印相应的行

时间:2018-09-27 13:02:35

标签: python python-3.x

我试图找到最大值(数据集中的人口),并使用Python标准库(没有Pandas)返回相应的行。当然,我必须将字符串输出映射为整数,这已经完成。无法弄清楚如何返回相应的行。这是我到目前为止的内容:

import csv

with open('gapminder.tsv', 'r') as gap:
    csv_reader = csv.reader(gap, delimiter='\t')
    pop = []
    next(csv_reader)
    for row in csv_reader:
        pop.append([row[4]])
        pop = [[int(x) for x in line] for line in pop]
        pop_max = max(pop)

print(pop_max)

我的输出是:

   [1318683096]

,并且必须为:

        country continent  year  lifeExp         pop    gdpPercap
    299   China      Asia  2007   72.961  1318683096  4959.114854

一些示例数据:

   country  continent   year    lifeExp pop gdpPercap
   Afghanistan  Asia    1952    28.801  8425333 779.4453145
   Afghanistan  Asia    1957    30.332  9240934 820.8530296
   Afghanistan  Asia    1962    31.997  10267083    853.10071
   Afghanistan  Asia    1967    34.02   11537966    836.1971382
   Afghanistan  Asia    1972    36.088  13079460    739.9811058
   Afghanistan  Asia    1977    38.438  14880372    786.11336
   Afghanistan  Asia    1982    39.854  12881816    978.0114388
   Afghanistan  Asia    1987    40.822  13867957    852.3959448
   Afghanistan  Asia    1992    41.674  16317921    649.3413952
   Afghanistan  Asia    1997    41.763  22227415    635.341351
   Afghanistan  Asia    2002    42.129  25268405    726.7340548
   Afghanistan  Asia    2007    43.828  31889923    974.5803384
   Albania  Europe  1952    55.23   1282697 1601.056136
   Albania  Europe  1957    59.28   1476505 1942.284244
   Albania  Europe  1962    64.82   1728137 2312.888958
   Albania  Europe  1967    66.22   1984060 2760.196931
   Albania  Europe  1972    67.69   2263554 3313.422188
   Albania  Europe  1977    68.93   2509048 3533.00391
   Albania  Europe  1982    70.42   2780097 3630.880722
   Albania  Europe  1987    72  3075321 3738.932735
   Albania  Europe  1992    71.581  3326498 2497.437901

1 个答案:

答案 0 :(得分:2)

使用csvmax以及适当的按键功能,您可以执行以下操作:

import sys, csv

with open('gapminder.tsv','r') as gap:
  csv_reader = csv.reader(gap, delimiter='\t')

  header = next(csv_reader)
  pop_max = max(csv_reader, key=lambda row: int(row[4]))

  # output tsv to console
  w = csv.writer(sys.stdout, delimiter='\t')

  w.writerow(header)  
  w.writerow(pop_max)