3 回答
TA贡献1802条经验 获得超4个赞
您可以使用json_normalize
:
df = pd.io.json.json_normalize(s)
print(df)
name points tags
0 first 0.5 [{'key': 'Owner', 'value': 'A'}]
1 first 1.5 [{'key': 'Owner', 'value': 'B'}]
2 first 24.0 [{'key': 'SomeOtherTag', 'value': 'XYZ'}]
# to filter
filter_mask = df['tags'].apply(lambda x: x[0]['value'] == 'A')
df.loc[filter_mask, "points"].sum()
TA贡献1802条经验 获得超5个赞
如果您不严格使用 Pandas,另一种方法是对生成器理解求和,假设tags
每行列表中只嵌入一个字典:
sum(entry["points"] for entry in data if entry["tags"][0]["value"] == "A") 0.5
TA贡献1757条经验 获得超7个赞
json_normalize()为你做这一切
js = [{'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'}]}]
pd.json_normalize(js, record_path="tags", meta=[['name'], ['points']])
输出
key value name points
Owner A first 0.5
Owner B first 1.5
SomeOtherTag XYZ first 24
补充更新
如果从文件中读取
import json
with open('all_items.json') as f: items = json.load(f)
pd.json_normalize(items, record_path="tags", meta=[['name'], ['points']])
添加回答
举报