2017-05-28 37 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