I have a DataFrame that contains pseudo nested columns (i.e., . as a name) made by using Panda's function 'json_normalize' in a nested JSON. I would like to build a JSON out of the DataFrame that has nested values.
Here's an example of what I'm trying to do. Note that this is just an example to make it more understandable, it's not exactly my use case. My use case requires Pandas to do some "heavy" Dataframe transformations.
DataFrame like:
id name.first name.last
0 1 Coleen Volk
1 2 Mose Regner
The JSON I want is:
[{'id': 1,
'name': {'first': 'Coleen',
'last': 'Volk'}},
{'id': 2,
'name': {'first': 'Mose',
'last': 'Regner'}}]
-- Simple code to replicate:
data = [{'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk', 'nickname': 'Perico'}},
{'id': 2, 'name': {'first': 'Mose', 'last': 'Regner', 'nickname': 'Palotes'}}]
df = pd.io.json.json_normalize(data)
#some transormations using Pandas
df = df.drop(columns=['name.nickname'])
#Now I want to build the JSON