首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用Pandas将API响应数据解析为Python中的数据帧

用Pandas将API响应数据解析为Python中的数据帧
EN

Stack Overflow用户
提问于 2018-07-29 04:29:24
回答 1查看 146关注 0票数 0

我正在尝试解析来自对ERP系统的API调用的数据。我想用Pandas将这些数据作为一个数据框架引入,这样我就可以使用这些数据了。使用json_string / json_dumps或DataFrame.from_dict()解析它的每一次尝试对我来说都不起作用。

我的原始数据文件如下所示:

代码语言:javascript
复制
Type:        dict
String form: {0: [{'productID': 144194, 'name': 'XXXtentacion, ?,        LP', 'code': '1039907', 'code2': '1210672',  <...> Field4Title': 'Format Notes', 'extraField4ID': 0, 'extraField4Code': '', 'extraField4Name': ''}]}
    Length:      1
    Docstring:  
    dict() -> new empty dictionary
    dict(mapping) -> new dictionary initialized from a mapping object's
        (key, value) pairs
    dict(iterable) -> new dictionary initialized as if via:
        d = {}
        for k, v in iterable:
            d[k] = v
    dict(**kwargs) -> new dictionary initialized with the name=value pairs
        in the keyword argument list.  For example:  dict(one=1, two=2)

我得到的最接近的电话是:

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

它返回:

代码语言:javascript
复制
                                                    0
0   {'productID': 144194, 'name': 'XXXtentacion, ?...
1   {'productID': 131605, 'name': 'Sufjan Stevens ...
2   {'productID': 143699, 'name': 'Sufjan Stevens ...
3   {'productID': 134277, 'name': 'Sufjan Stevens ...
4   {'productID': 135151, 'name': 'Sufjan Stevens ...
5   {'productID': 145844, 'name': 'Spearhead, Home...

但我想要的是键是列标题(即'productID‘应该是第一列标题。

我刚刚开始使用Python,所以非常感谢您的帮助。我在类似的话题上四处寻找,似乎找不到解决方案。

EN

回答 1

Stack Overflow用户

发布于 2018-07-29 05:22:07

假设您的数据结构为Dict(key1: List(Dict( ...) ),key2:...)

试一试

代码语言:javascript
复制
data = {d:data[d][0] for d in data}
pd.DataFrame.from_dict(data)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51575033

复制
相关文章

相似问题

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