0

Am new to python and have a nested dictionary from a CSV that looks like this:

data = {
         {object: a, type: 1, value: 1.50, another_value: 2}
         {object: b, type: 1, value: 3.67, another_value: 4}   
       }

I am trying print data from different columns if a value exists. First, I want to find the max number in all entries in the 'value' column. Then I want to print the 'object' if that max value exists within that dictionary.

So in this instance I want my output to look like:

print('Max value is 3.67 which is found in object b)

I've managed this far:

data = {see above}
max_list[]

for row in data:
    values = float(row['value'])
    max_list.append(values)

max_value = max(max_list)

if max_value in data
    max_object = data(['object'])

print('Max value: {} which is found in object {}'.format(max_value, max_object))

Any help would be appreciated! Thanks!

  • 3
    Your data structure is not valid. Please post a [mre]. – Asocia May 02 '21 at 11:14
  • i guess you need something like pandas for data manipulation and analysis – Vivek Anand May 02 '21 at 11:19
  • Does this answer your question? [In List of Dicts, find min() value of a common Dict field](https://stackoverflow.com/questions/5320871/in-list-of-dicts-find-min-value-of-a-common-dict-field) – sushanth May 02 '21 at 11:40

1 Answers1

0

Assuming that you made a mistake there

data = [
    {"object": "a", "type": 1, "value": 1.50, "another_value": 2},
    {"object": "b", "type": 1, "value": 3.67, "another_value": 4},
    {"object": "c", "type": 2, "value": 3.50, "another_value": 6},
]

sorted_data = sorted(data, key=lambda k: k['value']) 
maxx = sorted_data[-1]
print('Max value: {} which is found in object {}'.format(maxx['value'], maxx['object']))
Aven Desta
  • 2,114
  • 12
  • 27