gpt4 book ai didi

python - 如何查找包含 csv 文件中行的最高值的列名

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

我试图在遍历每一行并从该行中获取最高值后获取列标题,我该怎么做?

with open(filePath) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
euro = int(row['Euro'])
usd = int(row['Usd'])
pound = int(row['Pound'])
yuan= int(row['Yuan'])
max_curr = max(euro,usd,pound,yuan)

Example rows

例如。对于第一行数据,我想打印标题“Euro”,因为 99 是该行中的最大值

对于第二行,我想打印标题“Usd”,因为 99 是该行中的最大值

最佳答案

max()函数中使用key参数:

import csv
from collections import OrderedDict

with open(filePath) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# converting the row values to integers.
row = OrderedDict((k, int(v)) for k, v in row.items())
# getting the column name of the max value
max_curr = max(row, key=row.get)
print(max_curr)

关于python - 如何查找包含 csv 文件中行的最高值的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55778606/

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