learningtoprogram123
learningtoprogram123

Reputation: 79

Convert nested dictionary to csv

I have a particular case where I want to create a csv using the inner values of a nested dictionary as the keys and the inner keys as the header. The 'healthy' key can contain more subkeys other than 'height' and 'weight', but the 'unhealthy' will either ever contain None or a string of values. My current dictionary looks like this:

{0: {'healthy': {'height': 160,
                 'weight': 180},
    'unhealthy': None},
 1: {'healthy': {'height': 170,
                'weight': 250},
    'unhealthy': 'alcohol, smoking, overweight'}
}

How would I convert this to this csv:

+------+--------+----------------------------+
|height|  weight|                   unhealthy|
+------+--------+----------------------------+
|160   |     180|                            |
+------+--------+----------------------------+
|170   |     250|alcohol, smoking, overweight|
+------+--------+----------------------------+

Is there anyway of not hardcoding this and doing this without Pandas and saving it to a location?

Upvotes: 0

Views: 1549

Answers (3)

Park
Park

Reputation: 2484

I used pandas to deal with the value and to save it as a csv file.

  1. I loaded the json format data as a dataframe.
  2. By transposing the data, I got 'unhealty' columne
  3. By using json_normalize(), I parsed the nested dictionary data, 'healthy' and generated two columns into 'height' and 'weight'
  4. concat the 'healthy' and 'height', 'weight' columns
  5. saved the dataframe as a csv file.
import pandas as pd

val = {
    0: {'healthy': {'height': 160, 'weight': 180},
        'unhealthy': None},
    1: {'healthy': {'height': 170, 'weight': 250},
        'unhealthy': 'alcohol, smoking, overweight'}
}

df = pd.DataFrame.from_dict(val)
df = df.transpose()
df = pd.concat([pd.json_normalize(df['healthy'], max_level=1), df['unhealthy']], axis=1)
df.to_csv('filename.csv', index=False) # A csv file generated.

This is the csv file I generated (I opend it using MS Excel). This is the csv file

Upvotes: 0

sudden_appearance
sudden_appearance

Reputation: 2197

So this may be very dumb way to do this, but if your dictionary has this structure and you don't mind about hardcoding the actual values, this might be the way

import csv

dictionary = {0: {'healthy': {'height': 160,
                              'weight': 180},
                  'unhealthy': None},
              1: {'healthy': {'height': 170,
                              'weight': 250},
                  'unhealthy': 'alcohol, smoking, overweight'}
              }

with open("out.csv", "w") as f:
    writer = csv.writer(f)
    writer.writerow(['height', 'weight', 'unhealthy'])
    writer.writerows([
        [value['healthy']['height'],
         value['healthy']['weight'],
         value['unhealthy']
         ] for key, value in dictionary.items()])

So the point is you just create an array of [<height>, <weight>, <unhealthy>] arrays and write it to csv file using python's builtin module's csv.writer.writerows()

Upvotes: 1

user15398259
user15398259

Reputation:

With D being your dictionary you can pass D.values() to pandas.json_normalize() and rename the columns if needed.

>>> import pandas as pd
>>> print(pd.json_normalize(D.values()).to_markdown(tablefmt='psql'))
+╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+
|    | unhealthy                    |   healthy.height |   healthy.weight |
|╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌|
|  0 |                              |              160 |              180 |
|  1 | alcohol, smoking, overweight |              170 |              250 |
+╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+

Upvotes: 1

Related Questions