2017-06-21 18 views
1

私は、BestBuy Products APIからPythonのRequestsライブラリを使用していくつかのデータをダウンロードしており、それらをpandasデータフレームに保存したいと考えています。そのようなさまざまなフィールドリストを含むJSONレスポンスをPandasデータフレームに変換する

何か:

products['products'] 

私が持っている:

results = requests.get(url1, 
        params={'paramStuff'}, 
        headers={'User-Agent': ua}) 
products = json.loads(results.text) 

Aので、私は私がしたいJSON内の特定のフィールドを目指し、サービス情報と様々な分野の多くを得る

[{'details':[{'name': 'Name of Feature', 'value':'Value Of Feature'}, 
      {'name': 'Name of Other Feature', 'value':'Value Of Other 
       Feature'}, ...], 
    'ProductId': 'Id Of Product 1', 
    'Some Other Field': 'Some Other Field Value'}, 
{same structure as above for other product}, {etc}] 

このように、辞書のリストのようなもので、辞書のリスト自分自身。強調表示するには、詳細dictはName:Valueの組み合わせのさまざまなリストを持つことができます(名前は製品間でも異なります)。

、このような形式のデータフレームに入るために、このような構造にアプローチする方法上の任意のアイデア:

+-----------+-------------------+-------------------+-------------------+------------------+ 
| ProductID | Name of Feature 1 | Name of Feature 2 | Name Of Feature 3 | Some Other Field | 
+-----------+-------------------+-------------------+-------------------+------------------+ 
| Product 1 | Value    | NULL    | Value    | Value   | 
| Product 2 | NULL    | Value    | Value    | Value   | 
+-----------+-------------------+-------------------+-------------------+------------------+ 

は、これまでのところ私はこのような何かを得ることができた:

+-----------+-----------------------------------------------------------------------------------------------------------------------------------+------------------+ 
| ProductID |                Details                | Some Other Field | 
+-----------+-----------------------------------------------------------------------------------------------------------------------------------+------------------+ 
| Product 1 | [{'name': 'Name of Feature', 'value':'Value Of Feature'},{'name': 'Name of Other Feature', 'value':'Value Of Other Feature'},...] | Value 1   | 
| Product 2 | [{'name': 'Name of Feature', 'value':'Value Of Feature'},{'name': 'Name of Other Feature', 'value':'Value Of Other Feature'},...] | Value 2   | 
+-----------+-----------------------------------------------------------------------------------------------------------------------------------+------------------+ 
+0

あなたは 'pandas.read_json'で作業を試みましたか? – jhamman

+0

@jhammanはい、悲しいことに重い入れ子を扱うことができません。私はこの問題を解決するために、手動パーサーを書くことで終わりました。このパーサは、より良い方法がない場合、答えとしてここに投稿します。 –

答えて

1

[OK]を、私は終わりましたネストされたフィールドを手動で解析する方法を開発しています。簡単な方法があるかどうかは分かりませんでした。 FYIこれはBestBuy Products APIからの応答を解析するために使用されています。

#first build the pandas DF shown in question 
df = pd.io.json.json_normalize(products) 

#fields which are not nested and not require parsing 
fields = ['sku', 'name', 'regularPrice', 'manufacturer'] 
#nested field is called 'details', as mentioned can have a lot of different subfields 
featureFields = [] 


#first build a list which will have all potential features from the nested field 
for i in range(0,len(df)): 
    row = df.iloc[i] 
    for detail in row['details']: 
     featureFields.append(detail['name'].split('>', 1)[-1]) 

#make a list unique 
featureFields = set(featureFields)  
fields = set(fields) 

#now we go over each record in dataframe and parse nested field to a dict 
records = [] 

for i in range(0,len(df)): 
    row = df.iloc[i] 
    record = dict.fromkeys(fields) 
    record['name'] = row['name'] 
    record['regularPrice'] = row['regularPrice'] 
    record['manufacturer'] = row['manufacturer'] 
    record['sku'] = row['sku'] 
    for detail in row['details']: 
     record[detail['name'].split('>', 1)[-1]] = detail['value'].split('>', 1)[-1] 
    records.append(record) 

#finally we have not nested list of dictionaries with records 
dfFinal = pd.DataFrame.from_dict(records) 
関連する問題