user14757480
user14757480

Reputation: 11

pandas dataframe to json with each row encapsulated in a parent element

I'd like to use the to_json() function to serialize a pandas dataframe while encapsulating each row in a root 'Person' element.

import pandas as pd
data = [['tom', 10], ['nick', 15], ['juli', 14]]
df = pd.DataFrame(data, columns = ['Name', 'Age'])
df.to_json(orient='records')
'[{"Name":"tom","Age":10},{"Name":"nick","Age":15},{"Name":"juli","Age":14}]'

I'd like the to_json() output to be:

'[{"Person":{"Name":"tom","Age":10}},{"Person":{"Name":"nick","Age":15}},{"Person":{"Name":"juli","Age":14}}]'

I'm thinking this can be achieved with dataframe.apply() but haven't been able to figure it out.

Thx.

Upvotes: 1

Views: 1920

Answers (2)

Salma Elshahawy
Salma Elshahawy

Reputation: 1190

I suppose you want to use Person as an index or identifier for each set. Otherwise, just include Person as a fixed string key for each nested dict would be redundant. If this is the case, you can use index inside the orient argument. In this case, it would append the index associated with the data frame.

import pandas as pd
>>> data = [['tom', 10], ['nick', 15], ['juli', 14]]
>>> df = pd.DataFrame(data, columns = ['Name', 'Age'])
>>> temp = [df.to_json(orient='index')]
>>> temp = ['{"0":{"Name":"tom","Age":10},"1":{"Name":"nick","Age":15},"2":{"Name":"juli","Age":14}}']

Also, you can adjust your index to whatever you want. I hope this is what you want.

Upvotes: 0

Mayank Porwal
Mayank Porwal

Reputation: 34076

Use List Comprehension to create a list of dicts using df.to_dict:

In [4370]: d = [{'Person':i} for i in df.to_dict(orient='records')]

Convert above dict to json using json.dumps:

In [4372]: import json

In [4373]: j = json.dumps(d)

In [4374]: print(j)
Out[4373]: '[{"Person": {"Name": "tom", "Age": 10}}, {"Person": {"Name": "nick", "Age": 15}}, {"Person": {"Name": "juli", "Age": 14}}]'

Upvotes: 5

Related Questions