I have nested json data that resembles the following:
[{'game':'001', 'animals': [{'name':'Dog', 'colour':'Red'}, {'name':'Horse', 'age':'6'},{'name':'Ostrich', 'location':'Africa'}]},{'game':'002', 'animals': [{'name':'Cat', 'colour':'Green'}, {'name':'Bison', 'location':'North America'},{'name':'Parrot', 'location':'Southeast Asia'}]}]
My objective is to create an indicator array entry for each animal (contained in 'name') corresponding to items in the variable "animal_list":
animal_list = ['Bison', 'Cat', 'Dog', 'Elephants', 'Horse', 'Ostrich', 'Parrot']
So the desired structure would resemble (expressed as a csv...but this is illustrative only since an numpy positional array is what i'm seeking):
Game, Bison, Cat, Dog, Elephants, Horse, Ostrich, Parrot
"001",0,0,1,0,1,1,0
"002",1,1,0,0,0,0,1
I have traditionally formed this using a "double-loop" - first on 'game' items; followed by an inner loop that scans through the 'name' items. Problem is, I have a long json list and it is taking hours to run.
Thanks for your help!
jsonis a string;loadsmakes a dictionary. There are only 2 ways to access dictionary elements - by key indexing or viaitemslists.numpydoes not have an magic to do either of these faster.