我有如下json資料:
{"name"/span>: "Monkey"/span>, "image"/span>: "https://media.npr.org/assets/img/2017/09/12/macaca_nigra_self-portrait-3e0070aa19a7fe36e802253048411a38f14a79f8-s800-c85.webp", "屬性": [{"trait_type": "Bones"/span>, "value"/span>: "僵尸"}, {"trait_type": "Clothes", "value": "Striped"}, {"trait_type": "Mouth", "value": "Bubblegum"}, {"trait_type": "眼睛", "價值": "Black Sunglasses"}, {"trait_type": "帽子", "價值": "Sushi"}, {"trait_type": "Background", "value": "Purple"}]}。
我想把這個json資料轉換為pandas資料框架,只選擇屬性作為過濾器,如下所示:
Bones Clothes Mouth Eyes Hat Background
僵尸條紋 泡泡糖 黑色 壽司 紫色
是否有專家能幫助我獲得我提到的輸出? 謝謝你
uj5u.com熱心網友回復:
可能有一個更漂亮的解決方案,但這能完成作業:
import json
import pandas as pd
with open('file.json') as f。
trait_types= []
值 = []
data = json.load(f)
df = pd.DataFrame(data)
for key in data['attribute']:
trait_types.append(key['trait_type'] )
values.append(key['value'])
df = pd.DataFrame({
'trait type': trait_types,
'value' : values})
print(df)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/322805.html
標籤:
