python - 如何在csv文件中查找包含行的最大值的列名
问题描述
我试图在遍历每一行并从该行中获取最高值后获取列标题,我该怎么做?
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)
例如。对于第一行数据,我想打印标题“欧元”,因为 99 是该行中的最大值
对于第二行,我想打印标题“Usd”,因为 99 是该行中的最大值
解决方案
在函数中使用key
参数max()
:
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 - 将不同长度的列值拆分为新的列/特征
- java - 如何使用规范中的 CriteriaQuery 订购列表?
- python-3.x - 为什么在不同行的同一帧包会输出不同的东西?
- node.js - 在 Node js 中使用 JWT webtoken 处理身份验证的安全方法
- azure - Azure 自动缩放 - 手动预测自动缩放
- javascript - React forwardRef 含义
- javascript - 尝试从 Material UI 文本字段中获取日期值
- java - Java JSON(String) 映射到另一个不同的命名 JSON
- flutter - 如何在flutter中创建像ui一样的表格
- c# - C# .NET Core Console.ReadKey(true) 不会(总是)拦截 Ubuntu 上的密钥