[英]Print 2 common maximum values of a python dictionary
我已经将csv导入了熊猫的数据框。
Work Product Version
0 LCR_ContractualOutflowsMaster.aspx 1.1
1 LCR_CountryMaster.aspx 1.1
2 WBR_LCR_ContOutflowsMaster 1.0
3 USP_WBR_LCR_CountryMaster 1.0
然后将数据帧插入到python字典中。
{'LCR_ContractualOutflowsMaster.aspx': [1.1], 'LCR_CountryMaster.aspx': [1.1], 'WBR_LCR_ContOutflowsMaster': [1.0], 'USP_WBR_LCR_CountryMaster': [1.0]}
有两个具有共同最大值1.1
键。 有没有办法将这两个键打印到列表中?
我尝试了一些方法,例如(从某些堆栈溢出查询中引用)
1) max_value = max(csv_dict.items(), key=operator.itemgetter(1))[0]
2) max_value = max(csv_dict.items(), key=lambda x: x[1])[0]
3) max_value = max(csv_dict.values()); {key for key, value in csv_dict.items() if value == max_value}
4) max_value = max(csv_dict, key=csv_dict.get)
它仅打印一个值。
问候
1- df['Version']==df['Version'].max()
实际获取所有包含df['Version']==df['Version'].max()
记录。如数据框2所示,这是最大值,因此前两个记录为由于第一行代码而获取。
2- df['Work Product'].unique()
对照版本的最大值获取唯一的work_product
df = pd.DataFrame(data={"Work Product":["A","B","C","D"],
"Version":[2,2,1,1]})
df = df[df['Version'] ==df['Version'].max()]
uq_work_product = list(df['Work Product'].unique())
print(uq_work_product)
['A', 'B']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.