I have a very large, deeply nested json, from which I need only some key-values pairs, not all of them. Because it's very deeply nested, it's not comfortable to create a pandas dataframe directly from the json, because all the values I need will not be in columns.
I need to create pandas dataframe that should look like:
Groupe Id MotherName FatherName
Advanced 56 Laure James
Middle 11 Ann Nicolas
Advanced 6 Helen Franc
From my complex json I extract the values I need as follows (All the values are extracted correctly so there is no error here):
jfile=json.loads(data)
groupe=jfile['location']['groupe']
id=jfile['id']
MotherName=jfile['Mother']['MotherName']
FatherName=jfile['Father']['FatherName']
jfile looks like:
{"location":{"town":"Rome","groupe":"Advanced", "school":{"SchoolGroupe":"TrowMet", "SchoolName":"VeronM"}}, "id":"145", "Mother":{"MotherName":"Helen","MotherAge":"46"},"NGlobalNote":2, "Father":{"FatherName":"Peter","FatherAge":"51"}, "Teacher":["MrCrock","MrDaniel"],"Field":"Marketing","season":["summer","spring"]}
Then I create an empty dataframe and try to fill it with these values:
df=pd.DataFrame(columns=['group', 'id', 'Father', 'Mother'])
for index,row in df.iterrows():
row['groupe']=jfile['location']['groupe']
row['id']=jfile['id']
row['MotherName']=jfile['Father']['FatherName']
row['id']=jfile['Mother']['MotherName']
but when I try to print it, it says the dataframe is empty:
Empty DataFrame Columns: [groupe, id, MotherName, FatherName] Index: []
Why is it empty with this method, and how I can fill it properly?
If you're just trying to add a single row to your dataframe, you can use
df = df.append({"groupe":group,"id":id,"MotherName":MotherName,"FatherName":FatherName},
ignore_index=True)
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.