2

I am trying to convert a dataframe to a nested dictionary but no success so far.

Dataframe: clean_data['Model', 'Problem', 'Size']

Here's how my data looks like:

 Model                Problem             Size
 lenovo a6020         screen broken         1
 lenovo a6020a40      battery              60
                      bluetooth            60
                      buttons              60
 lenovo k4            wi-fi                 3
                      bluetooth             3

My desired output:

{
  "name": "Brand",
  "children": [
     {
         "name": "Lenovo",
         "children": [
             {
              "name": "lenovo a6020",
              "children": {
                  "name": "screen broken",
                  "size": 1
               }
             },
             {
              "name": "lenovo a6020a40",
              "children": [
                 {
                   "name": "battery",
                   "size": 60
                 },
                 {
                   "name": "bluetooth",
                   "size": 60
                 },
                 {
                   "name": "buttons",
                   "size": 60
                 }
               ]
             },
             {
              "name": "lenovo k4",
              "children": [
                {
                  "name": "wi-fi",
                  "size": 3
                },
                {
                  "name": "bluetooth",
                  "size": 3
                }
               ]
            }
         ]
      }
   ]
 }

I have tried pandas.DataFrame.to_dict method But it is returning a simple dictionary but I want it like the one mentioned above.

3
  • What is print(df.columns) ? Commented Sep 27, 2018 at 12:03
  • Could you please add code that generates your example dataframe? Commented Sep 27, 2018 at 12:06
  • Dataframe: clean_data['Model', 'Problem', 'Size'] I updated it, please see the edits Commented Sep 27, 2018 at 12:17

1 Answer 1

6

Use:

print (df)
             Model        Problem  size
0     lenovo a6020  screen broken     1
1  lenovo a6020a40        battery    60
2              NaN      bluetooth    60
3              NaN        buttons    60
4        lenovo k4          wi-fi     3
5              NaN      bluetooth     3

 #repalce missing values by forward filling
df = df.ffill()
#split Model column by first whitesapces to 2 columns 
df[['a','b']] = df['Model'].str.split(n=1, expand=True)

#each level convert to list of dictionaries
#for correct keys use rename
L = (df.rename(columns={'Problem':'name'})
        .groupby(['a','b'])['name','size']
        .apply(lambda x: x.to_dict('r'))
        .rename('children')
        .reset_index()
        .rename(columns={'b':'name'})
        .groupby('a')['name','children']
        .apply(lambda x: x.to_dict('r'))
        .rename('children')
        .reset_index()
        .rename(columns={'a':'name'})
        .to_dict('r')
        )
#print (L)

#create outer level by contructor
d = { "name": "Brand", "children": L}

print (d)

{
    'name': 'Brand',
    'children': [{
        'name': 'lenovo',
        'children': [{
            'name': 'a6020',
            'children': [{
                'name': 'screen broken',
                'size': 1
            }]
        }, {
            'name': 'a6020a40',
            'children': [{
                'name': 'battery',
                'size': 60
            }, {
                'name': 'bluetooth',
                'size': 60
            }, {
                'name': 'buttons',
                'size': 60
            }]
        }, {
            'name': 'k4',
            'children': [{
                'name': 'wi-fi',
                'size': 3
            }, {
                'name': 'bluetooth',
                'size': 3
            }]
        }]
    }]
}
Sign up to request clarification or add additional context in comments.

3 Comments

is it necessary to fill missing values? In actual, they are not missing values but nested.
@RohitSwami - Yes, if MultiIndex change df = df.ffill() to df = df.reset_index()
Exactly what I was looking for!! Thanks for help <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.