0

It is possible to use pandas.io.json.json_normalize to read from a nested json file. Is it possible to write a nested json from a pd.DataFrame where the columns have appropriate name?

MWE (from the doc of the linked page):

>>> from pandas.io.json import json_normalize
>>> data = [{'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk'}},
...         {'name': {'given': 'Mose', 'family': 'Regner'}},
...         {'id': 2, 'name': 'Faye Raker'}]
>>> df = json_normalize(data)
    id        name name.family name.first name.given name.last
0  1.0         NaN         NaN     Coleen        NaN      Volk
1  NaN         NaN      Regner        NaN       Mose       NaN
2  2.0  Faye Raker         NaN        NaN        NaN       NaN
>>> # Here I would like to do "to_json_normalize(df)" to recover "data"
ClementWalter
  • 4,814
  • 1
  • 32
  • 54
  • Check this out: https://stackoverflow.com/questions/40470954/convert-pandas-dataframe-to-nested-json – NYC Coder Jun 02 '20 at 19:59
  • I would like to avoid the painful series of `groupby` and `to_dict`. I would have guessed that following some template in the column name pandas could figure out how to convert the data – ClementWalter Jun 03 '20 at 10:44

0 Answers0