pandas from_json method usage

左心房为你撑大大i 提交于 2021-02-05 07:48:05

问题


I have a JSON file like below,

{ 
“A”:1,
“B”:2,
“C”: [
      {“x”:1,“y”:2,“z”:3},
      {"x":2,"y":7,"z":77}
     ]
}

pandas.from_json returns me data frame with column A,B and C. But, actually I am looking for columns with x,y and z. Is there a way to get it?


回答1:


You can use json_normalize:

json = { 
"A":1,
"B":2,
"C": [{"x":1,"y":2,"z":3 },
      {"x":2,"y":7,"z":77}]
}


from pandas.io.json import json_normalize    
df = json_normalize(json, 'C')

print (df)

   x  y   z
0  1  2   3
1  2  7  77

If need all columns:

df = json_normalize(json, 'C', ['A','B'])

print (df)
   x  y   z  B  A
0  1  2   3  2  1
1  2  7  77  2  1


来源:https://stackoverflow.com/questions/40100106/pandas-from-json-method-usage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!