简体   繁体   中英

Faster way to convert Pandas dataframe into nested json

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?

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM