My goal is to convert a dictionary to a Pandas dataframe using only json_normalize
.
What I have:
d = {'date': '20-NOV-2021', 'sector': {'South': 8, 'Est': 9, 'North': 12, 'Ouest': 9}}
json_normalize(d)
date | sector.South | sector.Est | sector.North | sector.Ouest
20-NOV-2021 | 8 | 9 | 12 | 9
What I'm looking for:
sector value date
South 8 20-NOV-2021
Est 9 20-NOV-2021
North 12 20-NOV-2021
Ouest 9 20-NOV-2021
I think you need to convert your sector
data into a slightly different format:
d['sector'] = [{'sector': k, 'value': v} for k, v in dd['sector'].items()]
df = pd.json_normalize(d, "sector", "date")
Output:
>>> df
sector value date
0 South 8 20-NOV-2021
1 Est 9 20-NOV-2021
2 North 12 20-NOV-2021
3 Ouest 9 20-NOV-2021
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.