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. 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|
------ -------- ----------------------------
CodePudding user response:
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 |
╌╌╌╌ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
CodePudding user response:
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()
CodePudding user response:
import pandas as pd
df = pd.DataFrame(d.values())
pd.concat([pd.DataFrame(df.healthy.to_list()), df.unhealthy], axis=1)
# height weight unhealthy
# 0 160 180 NaN
# 170 250 alcohol, smoking, overweight
CodePudding user response:
import csv
my_csv = 'health.csv'
health_data_new = {}
# csv field names
field_names = ['height', 'weight', 'unhealthy']
# original data
health_data = {0: {'healthy': {'height': 160,
'weight': 180},
'unhealthy': None},
1: {'healthy': {'height': 170,
'weight': 250},
'unhealthy': 'alcohol, smoking, overweight'}
}
# extract inner values - put in a loop or write directly to csv
health_data_new['height'] = health_data[0]['healthy']['height']
health_data_new['weight'] = health_data[0]['healthy']['weight']
health_data_new['unhealthy'] = health_data[0]['unhealthy']
# write new dict to csv
with open(my_csv, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=field_names)
writer.writeheader()
writer.writerows([health_data_new])