我有一个由API返回的json对象。我迭代了json数据并获得了一个字典列表。我想遍历这个字典列表并生成一个pandas数据帧。
json_data =
{'status': 'REQUEST_SUCCEEDED',
'responseTime': 136,
'message': [],
'Results': {'series': [{'seriesID': 'SMS04380600000000001',
'data': [{'year': '2020',
'period': 'M10',
'periodName': 'October',
'latest': 'true',
'value': '2148.7',
'footnotes': [{'code': 'P', 'text': 'Preliminary'}]},
{'year': '2020',
'period': 'M09',
'periodName': 'September',
'value': '2131.8',
'footnotes': [{}]},
{'year': '2020',
'period': 'M08',
'periodName': 'August',
'value': '2119.9',
'footnotes': [{}]},
{'year': '2020',
'period': 'M07',
'periodName': 'July',
'value': '2098.1',
'footnotes': [{}]},
]}
# Pandas DataFrame
df = pd.DataFrame()
# Iterate over data for each series
for series in json_data['Results']['series']:
for data in series['data']:
print(data)
# Populate Pandas DataFrame code goes here
df['Year'] ==
{'year': '2020', 'period': 'M10', 'periodName': 'October', 'latest': 'true', 'value': '2148.7',
'footnotes': [{'code': 'P', 'text': 'Preliminary'}]}
{'year': '2020', 'period': 'M09', 'periodName': 'September', 'value': '2131.8', 'footnotes': [{}]}
{'year': '2020', 'period': 'M08', 'periodName': 'August', 'value': '2119.9', 'footnotes': [{}]}
.
.
.此外,我需要从每个字典中删除footnotes键。
发布于 2020-11-25 23:13:00
https://stackoverflow.com/questions/65007420
复制相似问题