Python:将json结果更改为data.frame

问题描述 投票:0回答:1

在通过Python连接到F.A.C.E API并分析其中一个面部图像之后,我有了json对象。我是python中的新手,我的问题是:如何将json对象转换为data.frame,然后转换为.xls / .csv?

print ('Response : ', json_resp.text)
#Console print: 
Response :  {
  "error_code": 0,
  "description": "",
  "img_size": { "w": 650, "h": 488 },
  "people": [
  {
    "age": 22,
    "gender": 84,
    "mood": 29,
    "position": { "x": 190, "y": 161, "w": 259, "h": 259 },
    "rotation": { "yaw": -3, "pitch": 3, "roll": -1 },
    "landmarks": { "lefteye": { "x": 371, "y": 233 }, "righteye": { "x": 266, "y": 236 }, "maskpoints": [ { "x": 371, "y": 233 }, { "x": 266, "y": 236 }, { "x": 203, "y": 234 }, { "x": 206, "y": 261 }, { "x": 212, "y": 287 }, { "x": 220, "y": 313 }, { "x": 233, "y": 338 }, { "x": 250, "y": 357 }, { "x": 273, "y": 373 }, { "x": 296, "y": 388 }, { "x": 321, "y": 394 }, { "x": 346, "y": 390 }, { "x": 371, "y": 377 }, { "x": 396, "y": 362 }, { "x": 416, "y": 341 }, { "x": 430, "y": 315 }, { "x": 437, "y": 287 }, { "x": 444, "y": 258 }, { "x": 448, "y": 227 }, { "x": 215, "y": 221 }, { "x": 226, "y": 203 }, { "x": 247, "y": 195 }, { "x": 269, "y": 198 }, { "x": 291, "y": 204 }, { "x": 336, "y": 201 }, { "x": 360, "y": 193 }, { "x": 385, "y": 190 }, { "x": 408, "y": 198 }, { "x": 423, "y": 216 }, { "x": 314, "y": 231 }, { "x": 314, "y": 247 }, { "x": 314, "y": 263 }, { "x": 314, "y": 279 }, { "x": 294, "y": 297 }, { "x": 304, "y": 300 }, { "x": 315, "y": 302 }, { "x": 327, "y": 299 }, { "x": 338, "y": 297 }, { "x": 242, "y": 236 }, { "x": 254, "y": 226 }, { "x": 271, "y": 226 }, { "x": 284, "y": 240 }, { "x": 270, "y": 245 }, { "x": 253, "y": 244 }, { "x": 349, "y": 238 }, { "x": 362, "y": 224 }, { "x": 379, "y": 224 }, { "x": 393, "y": 234 }, { "x": 381, "y": 242 }, { "x": 363, "y": 242 }, { "x": 281, "y": 332 }, { "x": 294, "y": 327 }, { "x": 306, "y": 322 }, { "x": 315, "y": 325 }, { "x": 325, "y": 323 }, { "x": 340, "y": 328 }, { "x": 357, "y": 335 }, { "x": 341, "y": 347 }, { "x": 327, "y": 354 }, { "x": 317, "y": 354 }, { "x": 306, "y": 353 }, { "x": 294, "y": 347 }, { "x": 289, "y": 333 }, { "x": 306, "y": 331 }, { "x": 316, "y": 332 }, { "x": 325, "y": 331 }, { "x": 349, "y": 334 }, { "x": 326, "y": 339 }, { "x": 316, "y": 340 }, { "x": 306, "y": 339 } ] },
    "clothingcolors": [  ],
    "ethnicity": { "african": 83, "asian": 0, "caucasian": 12, "hispanic": 3 },
    "emotions": { "happiness": 1, "surprise": 5, "anger": 2, "disgust": 2, "fear": 1, "sadness": 11 }
  }
  ]
}

我想获得如下数据框:

       error_code, age, gender, mood, (...), emotions.sadness
0      0, 0, 84, 29, (...), 11

然后

“C:// Users”中的.xls或.csv

python json python-3.x pycharm
1个回答
1
投票

你可以使用方法pandas.read_jsonDataframe.to_csv

 import pandas

 df = pandas.read_json(json_resp.tex, typ='frame')
 df.to_csv("path_to_your_file.csv")
© www.soinside.com 2019 - 2024. All rights reserved.