0

I have data that looks like this:

player, goals, matches
ronaldo, 10, 5
messi, 7, 9

I want to convert this dataframe into a nested json, such as this one:

{
    "content":[
        {
            "player": "ronaldo",
            "events": {
                "goals": 10,
                "matches": 5
            }
        },
        {
            "player": "messi",
            "events": {
                "goals": 7,
                "matches": 9
            }
        }
    ]
}

This is my code, using list comprehension:

df = pd.DataFrame([['ronaldo', 10, 5], ['messi', 7, 9]], columns=['player', 'goals', 'matches'])
d = [{'events': df.loc[ix, ['goals', 'matches']].to_dict(), 'player': df.loc[ix, 'player']} for ix in range(df.shape[0])]
j = {}
j['content'] = d

This works, but the performance is really slow when I have a lot of data. Is there a faster way to do it?

Victor
  • 1,163
  • 4
  • 25
  • 45
  • Possible duplicate of [Convert Pandas Dataframe to nested JSON](https://stackoverflow.com/questions/40470954/convert-pandas-dataframe-to-nested-json) – Manvir Jul 25 '19 at 21:34

2 Answers2

0

Use pandas.to_json. Fast and easy https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html

df.T.to_json()

DeepBlue
  • 415
  • 4
  • 9
  • Would you share how you'd do it with `pandas.to_json`? I couldn't figure out how to squeeze `matches` and `goals` within `events`. – Victor Jul 25 '19 at 21:06
0

try :

df.to_json(orient = "records")

The problem is it doesn't stack goals and matches on the event column , I'm not sure though if you can do it without looping

Ayoub ZAROU
  • 2,387
  • 6
  • 20