首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历字典列表并创建pandas数据帧

遍历字典列表并创建pandas数据帧
EN

Stack Overflow用户
提问于 2020-11-25 23:09:17
回答 1查看 43关注 0票数 0

我有一个由API返回的json对象。我迭代了json数据并获得了一个字典列表。我想遍历这个字典列表并生成一个pandas数据帧。

代码语言:javascript
复制
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键。

EN

回答 1

Stack Overflow用户

发布于 2020-11-25 23:13:00

如果数据是您的字典列表,那么您只需执行以下操作:

代码语言:javascript
复制
df = pd.DataFrame(data)

this answer中所述。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65007420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档