使用Pandas查找特定列的“max”,然后使用同一行中的相应项输出该值

2024-10-08 18:31:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道如何找到列“CONTRCOST”的最大值,但如何获取相应的值并将这两条信息输入到字符串中,就像它要求我做的那样


Tags: 字符串信息contrcost
1条回答
网友
1楼 · 发布于 2024-10-08 18:31:24

我相信这会给你你想要的。idxmax()函数返回具有最大值的行的索引。如果有多个,则返回第一个

import pandas as pd

data = [{'id':0,'city':'Chicago','contrcost':1000000.00},
{'id':1,'city':'New York','contrcost':2000000.00},
{'id':2,'city':'Boston','contrcost':1000000.00},
{'id':3,'city':'Atlanta','contrcost':1000000.00},
{'id':4,'city':'Los Angeles','contrcost':2000000.00}]

df = pd.DataFrame(data)
max_index = df['contrcost'].idxmax()
print('City with the contract that has the greatest cost: {:s} (contract cost is ${:.2f})'
.format( df.iloc[max_index]['city'], df.iloc[max_index]['contrcost']))

产出:合同成本最高的城市:纽约(合同成本为2000000.00美元)

相关问题 更多 >

    热门问题