如何从 json 对象的嵌套列表中提取由值标识的列表?
How do I extract lists identified by value from nested list in json object?
非常感谢你的帮助。我正在使用 json_normalize 访问嵌套列表“terms”并提取下面数据中的“objectid”和“medium”值。
with open('file_name.txt','r', encoding = 'utf8') as f:
data=json.loads(f.read())
df = pd.json_normalize(data, record_path = ['terms'],meta=['objectid','medium'])
print(df)
我只想提取术语类型为“分类”和“材料”的列表,而不是提取所有列表。
"medium": "Handscroll; ink on paper",
"terms": [{
"term": "Chinese",
"aatid": 300018322,
"id": 2033355,
"termtype": "Culture"
},
{
"term": "scroll paintings",
"aatid": 300033666,
"id": 2053034,
"termtype": "Classification"
},
{
"term": "handscrolls",
"aatid": 300178463,
"id": 2053038,
"termtype": "Classification"
},
{
"term": "tigers",
"aatid": null,
"id": 2127399,
"termtype": "Subject"
},
{
"term": "paper (fiber product)",
"aatid": 300014109,
"id": 2043813,
"termtype": "Materials"
},
{
"term": "ink",
"aatid": 300015012,
"id": 2167672,
"termtype": "Materials"
}
],
"objectid": "138361",
我要打印:
term aatid id termtype objectid medium
handscrolls 300178463.0 2053038 Classification 138361 Handscroll; ink on paper
scroll paintings 300033666.0 2053034 Classification 138361 Handscroll; ink on paper
paper (fiber product) 300014109.0 2043813 Materials 138361 Handscroll; ink on paper
ink 300015012.0 2167672 Materials 138361 Handscroll; ink on paper
如果其他一切正常,这应该可以完成工作。您的数据无效json。首先使用 https://jsonlint.com/
验证您的 json
df.loc[(df.termtype == "Classification") | (df.termtype == "Materials")]
非常感谢你的帮助。我正在使用 json_normalize 访问嵌套列表“terms”并提取下面数据中的“objectid”和“medium”值。
with open('file_name.txt','r', encoding = 'utf8') as f:
data=json.loads(f.read())
df = pd.json_normalize(data, record_path = ['terms'],meta=['objectid','medium'])
print(df)
我只想提取术语类型为“分类”和“材料”的列表,而不是提取所有列表。
"medium": "Handscroll; ink on paper",
"terms": [{
"term": "Chinese",
"aatid": 300018322,
"id": 2033355,
"termtype": "Culture"
},
{
"term": "scroll paintings",
"aatid": 300033666,
"id": 2053034,
"termtype": "Classification"
},
{
"term": "handscrolls",
"aatid": 300178463,
"id": 2053038,
"termtype": "Classification"
},
{
"term": "tigers",
"aatid": null,
"id": 2127399,
"termtype": "Subject"
},
{
"term": "paper (fiber product)",
"aatid": 300014109,
"id": 2043813,
"termtype": "Materials"
},
{
"term": "ink",
"aatid": 300015012,
"id": 2167672,
"termtype": "Materials"
}
],
"objectid": "138361",
我要打印:
term aatid id termtype objectid medium
handscrolls 300178463.0 2053038 Classification 138361 Handscroll; ink on paper
scroll paintings 300033666.0 2053034 Classification 138361 Handscroll; ink on paper
paper (fiber product) 300014109.0 2043813 Materials 138361 Handscroll; ink on paper
ink 300015012.0 2167672 Materials 138361 Handscroll; ink on paper
如果其他一切正常,这应该可以完成工作。您的数据无效json。首先使用 https://jsonlint.com/
验证您的 jsondf.loc[(df.termtype == "Classification") | (df.termtype == "Materials")]