2017-12-11 8 views
-2

私はpandas.io.jsonを使用したので、jsonファイルからその説明に関連する 'description'と最初の 'x'、 'y'の値を取得しようとしていますページの最後にthis exampleに従ってもエラーを取得.json_normalizeと: KeyError例外:(「エラーで実行してみてください=キー%sが常に存在していないとして、 『無視』」、KeyError例外(「説明」、))jsonのネストされたリストと辞書の値を取得

どのようにすることができます次のjsonファイルからその記述(0,2)と(1,2)に関連する「説明」「再生」と「ゲーム」と最初の「x」、「y」の値を得て、結果をデータとして取得するフレーム?

私は、コードを編集して、私は、結果としてこれを取得したい:

0 1  2 3 
0  Play Game  
1   
2    
3    
4  

しかしゲームは、Xにする必要がありますyのではありません。

import pandas as pd 
from pandas.io.json import json_normalize 


data = [ 
      { 
      "responses": [ 
       { 
        "text": [ 
         { 
          "description": "Play", 
          "bounding": { 
           "vertices": [ 
            { 
             "x": 0, 
             "y": 2 
            }, 
            { 
             "x": 513, 
             "y": -5 
            }, 
            { 
             "x": 513, 
             "y": 73 
            }, 
            { 
             "x": 438, 
             "y": 73 
            } 
           ] 
          } 
         }, 
         { 
          "description": "Game", 
          "bounding": { 
           "vertices": [ 
            { 
             "x": 1, 
             "y": 2 
            }, 
            { 
             "x": 307, 
             "y": 29 
            }, 
            { 
             "x": 307, 
             "y": 55 
            }, 
            { 
             "x": 201, 
             "y": 55 
            } 
           ] 
          } 
         } 
        ] 
       } 
      ] 
     } 

    ] 
#w is columns h is rows 
w, h = 4, 5; 

Matrix = [[' ' for j in range(w)] for i in range(h)] 


for row in data: 
    for response in row["responses"]: 
     for entry in response["text"]: 
      Description = entry["description"] 
      x = entry["bounding"]["vertices"][0]["x"] 
      y = entry["bounding"]["vertices"][0]["y"] 
      Matrix[x][y] = Description 

df = pd.DataFrame(Matrix) 
print(df) 

答えて

0

私は、これはあなたが期待しているものであると思います

 x y description 
0 438 -5  Play 
1 513 -5  Play 
2 513 73  Play 
3 438 73  Play 
4 201 29  Game 
5 307 29  Game 
6 307 55  Game 
7 201 55  Game 

になりますそれはこの

df = json_normalize(data[0]['responses'][0]['text'],[['bounding','vertices']], 'description') 

ようjson_normalizeするdata[0]['responses'][0]['text']を渡す必要があります。

EDIT:

df.groupby('description').get_group('Play').iloc[0] 

はあなたにこれはすべてのxを返すグループ '遊び'

x    438 
y    -5 
description Play 
Name: 0, dtype: object 
+0

の最初の項目、その説明に関連のYを与えます。どのように最初のx、yだけを得ることができますか? – EqSan

+0

最新の編集をご覧ください。 – plasmon360

関連する問題