gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:32 28 4
gpt4 key购买 nike

我试图找到最大值(数据集中的人口)并使用 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

最佳答案

使用 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)

关于 python 3 : Find a max value in csv column and print corresponding row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52537649/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com