将类:str json转换为python中的pandas数据帧

时间:2017-05-28 16:41:36

标签: python json pandas dataframe

我有一个类:str JSON就像下面的输出一样,我希望将它转换为带有以下列的python pandas数据框。列名称为

    creator_id,
    creator_url,
    creator_first_name,
    creator_last_name,
    board_id,
    board_url,
    board_name,
    pin_id,
    pin_url,
    pin_type,
    pin_created_at,
    pin_original_link,
    pin_link,
    pin_note,
    pin_color,
    pin_likes,
    pin_comments,
    pin_repins,
    image_url,
    image_width,
    image_height,

类:str json输出如下所示:

{
   "data":[
      {
         "attribution":null,
         "creator":{
            "url":"s://www.pinterest.com/Roger7/",
            "first_name":"Roger",
            "last_name":"",
            "id":"450782381360556043"
         },
         "color":"#10321e",
         "media":{
            "type":"image"
         },
         "created_at":"2017-05-18T10:51:52",
         "original_link":"://www.ebaumsworld.com/pictures/view/82308675/",
         "note":"capilano suspension bridge - vancouver, british columbia",
         "link":"s://www.pinterest.com/r/pin/450782243942648204/4779055074072594921/90924faee8b4a396e0dfbf31e20598b4173da3512012b91d8a81a77dbdb3bfa9",
         "board":{
            "url":"s://www.pinterest.com/Roger7/places-to-go/",
            "id":"450782312641650320",
            "name":"Places to Go"
         },
         "image":{
            "original":{
               "url":"s://s-media-cache-ak0.pinimg.com/originals/fb/0a/5d/fb0a5da592f0c9ba4fa5f1cbe89cef23.jpg",
               "width":680,
               "height":447
            }
         },
         "counts":{
            "likes":0,
            "comments":0,
            "repins":0
         },
         "id":"450782243942648204",
         "metadata":{
            "place":{
               "category":"State",
               "name":"British Columbia",
               "locality":null,
               "country":"Canada",
               "region":null,
               "longitude":-125.0032,
               "source_url":"s://foursquare.com/v/british-columbia/53111609e4b03443dd8495e5",
               "street":null,
               "postal_code":null,
               "latitude":53.99983
            },
            "link":{
               "locale":"en",
               "title":"Amazing Places Around The World",
               "site_name":"ebaumsworld.com",
               "description":"Breath taking pictures from around the world.",
               "favicon":"s://s-media-cache-ak0.pinimg.com/favicons/7dbedbdeabe8775a648605a16d077df16d1339789db4c8ab869a7d80.ico?9d315554a045ab3373fad06fa3e1b7b8"
            },
            "article":{
               "published_at":null,
               "description":"Breath taking pictures from around the world.",
               "name":"Amazing Places Around The World",
               "authors":[
                  {
                     "name":"Rawrzorz"
                  }
               ]
            }
         }
      },
      {
         "attribution":{
            "title":"blue river in purple forest",
            "provider_favicon_url":"s://s.pinimg.com/images/api/attrib/getty images.png",
            "author_name":"aodaodaod",
            "url":"://www.thinkstockphotos.com/image/450637293",
            "author_url":"://www.thinkstockphotos.com/image/450637293",
            "provider_name":"Getty Images"
         },
         "creator":{
            "url":"s://www.pinterest.com/Roger7/",
            "first_name":"Roger",
            "last_name":"",
            "id":"450782381360556043"
         },
         "color":"#644668",
         "media":{
            "type":"image"
         },
         "created_at":"2017-05-18T10:51:37",
         "original_link":"://indiasinsights.com/fr/2015/09/02/50-places-around-the-world/",
         "note":"La rivi\u00e8re f\u00e9erique de Shotover River, en Nouvelle-Z\u00e9lande",
         "link":"s://www.pinterest.com/r/pin/450782243942648201/4779055074072594921/fa8a06f35e7ab53f93e6b66a1d639b41b1309e79a8e10bf95caf416f7d2b1a77",
         "board":{
            "url":"s://www.pinterest.com/Roger7/places-to-go/",
            "id":"450782312641650320",
            "name":"Places to Go"
         },
         "image":{
            "original":{
               "url":"s://s-media-cache-ak0.pinimg.com/originals/a6/6f/90/a66f905e9311b07666a6e2f83a6ea60c.jpg",
               "width":660,
               "height":982
            }
         },
         "counts":{
            "likes":0,
            "comments":0,
            "repins":0
         },
         "id":"450782243942648201",
         "metadata":{

         }
      }
   ],
   "page":{
      "cursor":null,
      "next":null
   }
}

1 个答案:

答案 0 :(得分:1)

数据条目中的词典列表可以使用以下函数进行解析:

pandas.io.json.json_normalize()

它将自动使用正确的名称放置嵌套字典。 例如,创建者dict将如下所示:

creator.url, creator.first_name, creator.last_name, creator.id

这将是显示在DataFrame中的列。

然后你只需要删除你不想要的列。