Steve Power
Steve Power

Reputation: 73

Python3: Find a max value in csv column and print corresponding row

I'm trying to find the largest value (population in a dataset) and return the corresponding row using Python standard library (no Pandas). Of course I have to map the string output to integers, which i've done. Can't figure out how to return the corresponding row. Here's what I have so far:

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)

My output is:

   [1318683096]

and needs to be:

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

some sample data:

   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

Upvotes: 2

Views: 418

Answers (1)

user2390182
user2390182

Reputation: 73460

Using csv and max with an appropriate key function, you can do the following:

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) 

Upvotes: 2

Related Questions