我有一个 JSON 文件结构化列表,所以:
[
{
"name": "first",
"points": 0.5,
"tags": [{
"key": "Owner",
"value": "A"
}]
},
{
"name": "first",
"points": 1.5,
"tags": [{
"key": "Owner",
"value": "B"
}]
},
{
"name": "first",
"points": 24,
"tags": [{
"key": "SomeOtherTag",
"value": "XYZ"
}]
}
]
我想加载这个 JSON 数组,然后过滤特定key: value标签并计算points. 例如,过滤这个“主”列表,然后Owner: A计算应该给我结果的点数0.5
我知道如何计算点数,items.points.sum()但我正在努力按标签键/值进行过滤。
这是我到目前为止所拥有的:
import pandas as pd
items = pd.read_json('all_items.json')
# Do something to filter down the list
filtered_items = ?????
print(filtered_items.points.sum())
根据 Rob 的解决方案进行编辑:
#pipenv install pandas
# pandas = 1.1.0
import pandas as pd
items = pd.read_json('all_items.json')
pd.json_normalize(items, record_path="tags", meta=[['name'], ['points']])
给TypeError: string indices must be integers。
慕虎7371278
慕后森
长风秋雁
相关分类