Python Forum

Full Version: When dictionarys do no have same keys
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to accessing nested dictionaries and here is the problem I have. One of my columns in a panda frame contains dictionaries. These dictionaries have as a first key ta number, that is not sequential.

To simplify the problem I print just the first entry of the panda frame column.
print(df.report[0])
Quote:{'253.0': {'precision': 0.8333333333333334, 'recall': 0.375, 'f1-score': 0.5172413793103448, 'support': 80}, '305.0': {'precision': 0.8132780082987552, 'recall': 0.9655172413793104, 'f1-score': 0.882882882882883, 'support': 203}, '308.0': {'precision': 0.9594594594594594, 'recall': 0.9861111111111112, 'f1-score': 0.9726027397260274, 'support': 216}, 'accuracy': 0.8797595190380761, 'macro avg': {'precision': 0.8686902670305159, 'recall': 0.7755427841634739, 'f1-score': 0.7909090006397518, 'support': 499}, 'weighted avg': {'precision': 0.8797702316524192, 'recall': 0.8797595190380761, 'f1-score': 0.8630996540097691, 'support': 499}}

In that dictionary the keys are:

253 305.0 308.0

I would like to use those keys to select the next available nested-dictionary to access the precision values.

I highlighted with bold the keys and the precision value I would like to get. If this makes it easier I would be still happy with just the precision values returned as a list. Where I am stuck is that in python I get do something like get to pick keys but my keys are not fixed.

Can you help me with that?

Regards Alex
How are you using the dictionary? If you want to retrieve all the precision values, you can iterate over the dictionary to access every key.
This will not work in my case
 for key, value in a_dict.items():
        print(key, '->', value)
since the value does not bring precision directly that I want but one more dictionary.
deadeye@nexus ~ $ ipython                                                                                                                                                                   
Python 3.8.0 (default, Oct 15 2019, 17:33:09) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: data = {'253.0': {'precision': 0.8333333333333334, 'recall': 0.375, 'f1-score': 0.5172413793103448, 'support': 80}, '305.0': {'precision': 0.8132780082987552, 'recall': 0.965517241
   ...: 3793104, 'f1-score': 0.882882882882883, 'support': 203}, '308.0': {'precision': 0.9594594594594594, 'recall': 0.9861111111111112, 'f1-score': 0.9726027397260274, 'support': 216}, '
   ...: accuracy': 0.8797595190380761, 'macro avg': {'precision': 0.8686902670305159, 'recall': 0.7755427841634739, 'f1-score': 0.7909090006397518, 'support': 499}, 'weighted avg': {'preci
   ...: sion': 0.8797702316524192, 'recall': 0.8797595190380761, 'f1-score': 0.8630996540097691, 'support': 499}}                                                                           

In [2]: data.keys()                                                                                                                                                                         
Out[2]: dict_keys(['253.0', '305.0', '308.0', 'accuracy', 'macro avg', 'weighted avg'])

In [3]: data['253.0']                                                                                                                                                                       
Out[3]: 
{'precision': 0.8333333333333334,
 'recall': 0.375,
 'f1-score': 0.5172413793103448,
 'support': 80}

In [4]: data.keys() - ('accuracy', 'macro avg', 'weighted avg')                                                                                                                             
Out[4]: {'253.0', '305.0', '308.0'}

In [5]: for value, subdict in data.keys() - ('accuracy', 'macro avg', 'weighted avg'): 
   ...:     print(f'{value} -> {subdict}') 
   ...:                                                                                                                                                                                     
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-6c08b0dd40e7> in <module>
----> 1 for value, subdict in data.keys() - ('accuracy', 'macro avg', 'weighted avg'):
      2     print(f'{value} -> {subdict}')
      3 

ValueError: too many values to unpack (expected 2)

In [6]: for key in data.keys() - ('accuracy', 'macro avg', 'weighted avg'): 
   ...:     print(f'{key} -> {data[key]}') 
   ...:      
   ...:                                                                                                                                                                                     
308.0 -> {'precision': 0.9594594594594594, 'recall': 0.9861111111111112, 'f1-score': 0.9726027397260274, 'support': 216}
305.0 -> {'precision': 0.8132780082987552, 'recall': 0.9655172413793104, 'f1-score': 0.882882882882883, 'support': 203}
253.0 -> {'precision': 0.8333333333333334, 'recall': 0.375, 'f1-score': 0.5172413793103448, 'support': 80}

In [7]: for key in data.keys() - ('accuracy', 'macro avg', 'weighted avg'): 
   ...:     print(f'{key} -> Precision: {data[key]['precision']}') 
   ...:  
   ...:                                                                                                                                                                                     
  File "<ipython-input-7-2e54305704fb>", line 2
    print(f'{key} -> Precision: {data[key]['precision']}')
                                            ^
SyntaxError: invalid syntax


In [8]: for key in data.keys() - ('accuracy', 'macro avg', 'weighted avg'): 
   ...:     print(f'{key} -> Precision: {data[key]["precision"]}') 
   ...:  
   ...:                                                                                                                                                                                     
308.0 -> Precision: 0.9594594594594594
305.0 -> Precision: 0.8132780082987552
253.0 -> Precision: 0.8333333333333334
Just play with you dict. You have keys, which are numbers as str and 3 other keys.