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?