2017-05-28 17 views
0

私は以下のような出力を持つJSONクラスを持っています。これを以下の列のpython pandasデータフレームに変換します。列名は、クラスclass: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 
    } 
} 
+0

myDataFrame = pd.read_json()がエラーを表示しています。ValueError:系列以外とのディクショナリを混同すると、あいまいな順序が発生する可能性があります。 –

+1

そのエラーをあなたの投稿、Rogerに追加しますか?コメントは、投稿に補遺ではなく明確化の質問のためのものであり、編集で最もうまくいきます。 – halfer

答えて

1

データエントリ内の辞書のリストは、機能を解析することができます。

pandas.io.json.json_normalize()

ネストされた辞書は、正しい名前で自動的に配置されます。 たとえば、作成者の辞書には、次のようになります。

あなたDATAFRAMEに表示列になります

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

関連する問題