I can't figure out how to generate this output:
"name" "hp" "hpperlevel"
"1" Annie 524 88
"2" Olaf 597.24 93
With this:
{
"type": "champion",
"version": "7.24.1",
"data": {
"1": {
"title": "the Dark Child",
"stats": {
"armorperlevel": 4,
"attackdamage": 50.41,
"mpperlevel": 50,
"attackspeedoffset": 0.08,
"mp": 334,
"armor": 19.22,
"hp": 524,
"hpregenperlevel": 0.55,
"attackspeedperlevel": 1.36,
"attackrange": 575,
"movespeed": 335,
"attackdamageperlevel": 2.625,
"mpregenperlevel": 0.8,
"critperlevel": 0,
"spellblockperlevel": 0.5,
"crit": 0,
"mpregen": 6,
"spellblock": 30,
"hpregen": 5.424,
"hpperlevel": 88
},
"id": 1,
"key": "Annie",
"name": "Annie"
},
"2": {
"title": "the Berserker",
"stats": {
"armorperlevel": 3,
"attackdamage": 68,
"mpperlevel": 42,
"attackspeedoffset": -0.1,
"mp": 315.6,
"armor": 35,
"hp": 597.24,
"hpregenperlevel": 0.9,
"attackspeedperlevel": 2.7,
"attackrange": 125,
"movespeed": 350,
"attackdamageperlevel": 3.5,
"mpregenperlevel": 0.575,
"critperlevel": 0,
"spellblockperlevel": 1.25,
"crit": 0,
"mpregen": 7.466,
"spellblock": 32.1,
"hpregen": 8.512,
"hpperlevel": 93
},
"id": 2,
"key": "Olaf",
"name": "Olaf"
},
The goal is DataFrame the first table with the JSON response (the second code) in Python pandas, or if I need to do something with the JSON response first i will be happy to know a hint.
The only method I know is pandas.DataFrame(Data["data"])
but the output is not the desired, yet i don't want to make a pandas.DataFrame(Data["data"]["1"]["stats"], index = [0], columns = ["hp", "hpperlevel"])
more than 100 times (the total numbers from the API) multiplied by the groups I want to make and merge them all.
The final output of my program is save the data as a excel file but the only big problem is make the groups first.
Any response will be greatly appreciated!
You can create a single dataframe with a couple of list comprehensions passed to the dataframe constructor:
import pandas
def get_stats(data):
return (data['id'], data['name'], data['stats']['hp'], data['stats']['hpperlevel'])
response = {
"data": {
"1": {
"stats": {"hp": 524, "hpperlevel": 88},
"id": 1,
"name": "Annie"
},
"2": {
"stats": {"hp": 597.24, "hpperlevel": 93},
"id": 2,
"name": "Olaf"
}
}
}
df = pandas.DataFrame(
data=[get_stats(response['data'][key]) for key in response['data']],
index=[int(key) for key in response['data']],
columns=['id', 'name', 'hp', 'hpperlevel']
).set_index('id')
And that gives me:
name hp hpperlevel
id
1 Annie 524.00 88
2 Olaf 597.24 93
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.