简体   繁体   中英

Print 2 common maximum values of a python dictionary

I have imported a csv to pandas' data frame.

                         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

Then then data frame was inserted in to a python dictionary.

{'LCR_ContractualOutflowsMaster.aspx': [1.1], 'LCR_CountryMaster.aspx': [1.1], 'WBR_LCR_ContOutflowsMaster': [1.0], 'USP_WBR_LCR_CountryMaster': [1.0]}

There are two keys which have common maximum value 1.1 . Is there a way to print out these two keys into a list?

I have tried some methods such as (referred from some stack overflow queries)

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)

It is only printing one value.

Regards

1- df['Version']==df['Version'].max() this actually fetch all records which contains maximum value of version.As you can see by dataframe 2 is a maximum value so the first two records would be fetched due to first line of code.

2- df['Work Product'].unique() this fetch unique work_product against maximum value of version

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM