打印python字典的2个常见最大值

时间:2019-07-23 15:10:50

标签: python pandas 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 个答案:

答案 0 :(得分:1)

1- df['Version']==df['Version'].max()实际上会提取所有包含version最大值的记录。如数据框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']
相关问题