Atti
Atti

Reputation: 410

python getting min/max values from nested dictionary

How can I get min/max for a value from a nested dictionary that also has 'Nan' for the missing values ?

*This is for reference i found a solution to this and i thought i`d share it here since i could not find an answer anywhere on stackoverflow.

Example dataset (i was working with the Enron dataset):

{'METTS MARK': {'salary': 365788, 'to_messages': 807, 'deferral_payments': 'NaN', 'total_payments': 1061827, 'exercised_stock_options': 'NaN', 'bonus': 600000, ...

Upvotes: 3

Views: 2535

Answers (2)

kotolotto
kotolotto

Reputation: 46

from udacity forum, nice solution

exercised_stock_options = [item["exercised_stock_options"] for k, item in 
data_dict.iteritems() if not item["exercised_stock_options"] == "NaN"]
print "min is %s" % min(exercised_stock_options)
print "max is %s" % max(exercised_stock_options)

Upvotes: 3

Atti
Atti

Reputation: 410

This is how i solved this issue:

#get minimum and maximum stock options

result = min(data_dict.values(), key=lambda v:v['exercised_stock_options'] if v['exercised_stock_options'] != 'NaN' else float('inf'))
print result

result = max(data_dict.values(), key=lambda v:v['exercised_stock_options'] if v['exercised_stock_options'] != 'NaN' else float('-inf'))
print result

Upvotes: 3

Related Questions