2017-05-28 39 views
0

我有一个类:str JSON类似于下面的输出,我想将它转换为一个python pandas数据框和下列列。列名会转换类:str json到python中的熊猫数据框

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 
    } 
} 
+0

myDataFrame = pd.read_json()给我一个错误ValueError:混合字符串与非系列可能会导致模糊排序。 –

+1

罗杰,你会将这个错误添加到你的帖子中吗?评论是为了澄清问题,而不是对帖子的补充 - 它们最好是在编辑中完成。 – halfer

回答

1

数据录入内部字典的名单可以用函数解析:

pandas.io.json.json_normalize()

它会自动将嵌套字典与正确的名称。 例如,创作者字典的样子:

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

,这将是您的数据框显示的列。

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

+0

++要更精确一点:'pandas.io.json.json_normalize(json.loads(json_string)['data'])' – MaxU

+0

感谢百万...它的工作原理! –

+0

@RogerBinny你可以将这个问题标记为回答:) – JAntunes