Mohammad Zain Abbas
Mohammad Zain Abbas

Reputation: 778

Python - Convert dict into nested dict

I have a dict:

{'Logistic Regression': u'                                precision    recall  f1-score   support\n\n              APAR Information       0.74      1.00      0.85       844\nAffected Products and Versions       0.00      0.00      0.00        18\n                        Answer       0.00      0.00      0.00        30\n   Applicable component levels       0.96      0.85      0.90       241\n             Error description       0.48      0.56      0.52       754\n                     Local fix       0.89      0.03      0.06       266\n                Modules/Macros       0.96      0.87      0.91       326\n                       Problem       0.00      0.00      0.00        63\n               Problem summary       0.51      0.73      0.60       721\n           Related information       0.00      0.00      0.00        22\n         Resolving The Problem       0.00      0.00      0.00        60\n                 Temporary fix       0.00      0.00      0.00        32\n                  circumvenion       0.00      0.00      0.00       124\n                     component       0.00      0.00      0.00        49\n                 temporary_fix       0.00      0.00      0.00         2\n\n                     micro avg       0.64      0.64      0.64      3552\n                     macro avg       0.30      0.27      0.26      3552\n                  weighted avg       0.60      0.64      0.58      3552\n'}

or

                                precision    recall  f1-score   support

              APAR Information       0.74      1.00      0.85       844
Affected Products and Versions       0.00      0.00      0.00        18
                        Answer       0.00      0.00      0.00        30
   Applicable component levels       0.96      0.85      0.90       241
             Error description       0.48      0.56      0.52       754
                     Local fix       0.89      0.03      0.06       266
                Modules/Macros       0.96      0.87      0.91       326
                       Problem       0.00      0.00      0.00        63
               Problem summary       0.51      0.73      0.60       721
           Related information       0.00      0.00      0.00        22
         Resolving The Problem       0.00      0.00      0.00        60
                 Temporary fix       0.00      0.00      0.00        32
                  circumvenion       0.00      0.00      0.00       124
                     component       0.00      0.00      0.00        49
                 temporary_fix       0.00      0.00      0.00         2

                     micro avg       0.64      0.64      0.64      3552
                     macro avg       0.30      0.27      0.26      3552
                  weighted avg       0.60      0.64      0.58      3552

and I want to convert this dict into a nested dict, something like,

{'Logistic Regression':
{'APAR Information':'0.74','1.00','0.85','844'},
{'Affected Products and Versions':'0.00','0.00','0.00','18'}
.
.
.}

How to achieve this ? Can it be done via dict build-in functions ?

Upvotes: 2

Views: 120

Answers (2)

jpp
jpp

Reputation: 164653

You can use 3rd party Pandas to convert to a dataframe via pd.read_fwf ("fixed-width formatted"). Your data is messy, you may need to write logic to calculate column widths or add them manually. Given an input dictionary d:

from io import StringIO
import pandas as pd

df = pd.read_fwf(StringIO(d['Logistic Regression']), widths=[30, 11, 10, 10, 10])\
       .dropna().rename(columns={'Unnamed: 0': 'index'}).set_index('index')

print(df)

                                precision  recall  f1-score  support
index                                                               
APAR Information                     0.74    1.00      0.85    844.0
Affected Products and Versions       0.00    0.00      0.00     18.0
Answer                               0.00    0.00      0.00     30.0
Applicable component levels          0.96    0.85      0.90    241.0
Error description                    0.48    0.56      0.52    754.0
Local fix                            0.89    0.03      0.06    266.0
Modules/Macros                       0.96    0.87      0.91    326.0
Problem                              0.00    0.00      0.00     63.0
Problem summary                      0.51    0.73      0.60    721.0
Related information                  0.00    0.00      0.00     22.0
Resolving The Problem                0.00    0.00      0.00     60.0
Temporary fix                        0.00    0.00      0.00     32.0
circumvenion                         0.00    0.00      0.00    124.0
component                            0.00    0.00      0.00     49.0
temporary_fix                        0.00    0.00      0.00      2.0
micro avg                            0.64    0.64      0.64   3552.0
macro avg                            0.30    0.27      0.26   3552.0
weighted avg                         0.60    0.64      0.58   3552.0

Then use a dictionary comprehension:

res = {'Logistic Regression': {idx: df.loc[idx].tolist() for idx in df.index}}

print(res)

{'Logistic Regression':
 {'APAR Information': [0.74, 1.0, 0.85, 844.0],
  'Affected Products and Versions': [0.0, 0.0, 0.0, 18.0],
  'Answer': [0.0, 0.0, 0.0, 30.0],
  'Applicable component levels': [0.96, 0.85, 0.9, 241.0],
  'Error description': [0.48, 0.56, 0.52, 754.0],
  'Local fix': [0.89, 0.03, 0.06, 266.0],
  'Modules/Macros': [0.96, 0.87, 0.91, 326.0],
  'Problem': [0.0, 0.0, 0.0, 63.0],
  'Problem summary': [0.51, 0.73, 0.6, 721.0],
  'Related information': [0.0, 0.0, 0.0, 22.0],
  'Resolving The Problem': [0.0, 0.0, 0.0, 60.0],
  'Temporary fix': [0.0, 0.0, 0.0, 32.0],
  'circumvenion': [0.0, 0.0, 0.0, 124.0],
  'component': [0.0, 0.0, 0.0, 49.0],
  'macro avg': [0.3, 0.27, 0.26, 3552.0],
  'micro avg': [0.64, 0.64, 0.64, 3552.0],
  'temporary_fix': [0.0, 0.0, 0.0, 2.0],
  'weighted avg': [0.6, 0.64, 0.58, 3552.0]}}

Upvotes: 1

Rakesh
Rakesh

Reputation: 82765

This is one approach.

Demo:

d = {'Logistic Regression': u'                                precision    recall  f1-score   support\n\n              APAR Information       0.74      1.00      0.85       844\nAffected Products and Versions       0.00      0.00      0.00        18\n                        Answer       0.00      0.00      0.00        30\n   Applicable component levels       0.96      0.85      0.90       241\n             Error description       0.48      0.56      0.52       754\n                     Local fix       0.89      0.03      0.06       266\n                Modules/Macros       0.96      0.87      0.91       326\n                       Problem       0.00      0.00      0.00        63\n               Problem summary       0.51      0.73      0.60       721\n           Related information       0.00      0.00      0.00        22\n         Resolving The Problem       0.00      0.00      0.00        60\n                 Temporary fix       0.00      0.00      0.00        32\n                  circumvenion       0.00      0.00      0.00       124\n                     component       0.00      0.00      0.00        49\n                 temporary_fix       0.00      0.00      0.00         2\n\n                     micro avg       0.64      0.64      0.64      3552\n                     macro avg       0.30      0.27      0.26      3552\n                  weighted avg       0.60      0.64      0.58      3552\n'}
result = {}
for i, v in enumerate(d["Logistic Regression"].splitlines()):
    if i == 0:
        continue
    val = v.strip().split("       ")
    if val[0]:
        result[val[0]] = " ".join(val[1:]).split()

for k, v in result.items():
    print(k)
    print(v)

Output:

weighted avg
[u'0.60', u'0.64', u'0.58', u'3552']
Local fix
[u'0.89', u'0.03', u'0.06', u'266']
Affected Products and Versions
[u'0.00', u'0.00', u'0.00', u'18']
component
[u'0.00', u'0.00', u'0.00', u'49']
Resolving The Problem
[u'0.00', u'0.00', u'0.00', u'60']
Error description
[u'0.48', u'0.56', u'0.52', u'754']
Problem summary
[u'0.51', u'0.73', u'0.60', u'721']
macro avg
[u'0.30', u'0.27', u'0.26', u'3552']
Related information
[u'0.00', u'0.00', u'0.00', u'22']
Applicable component levels
[u'0.96', u'0.85', u'0.90', u'241']
micro avg
[u'0.64', u'0.64', u'0.64', u'3552']
Answer
[u'0.00', u'0.00', u'0.00', u'30']
APAR Information
[u'0.74', u'1.00', u'0.85', u'844']
Problem
[u'0.00', u'0.00', u'0.00', u'63']
Modules/Macros
[u'0.96', u'0.87', u'0.91', u'326']
temporary_fix
[u'0.00', u'0.00', u'0.00', u'2']
circumvenion
[u'0.00', u'0.00', u'0.00', u'124']
Temporary fix
[u'0.00', u'0.00', u'0.00', u'32']

Upvotes: 2

Related Questions