我正在尝试解析来自对ERP系统的API调用的数据。我想用Pandas将这些数据作为一个数据框架引入,这样我就可以使用这些数据了。使用json_string / json_dumps或DataFrame.from_dict()解析它的每一次尝试对我来说都不起作用。
我的原始数据文件如下所示:
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)
我得到的最接近的电话是:
pd.DataFrame.from_dict(data)
它返回:
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,所以非常感谢您的帮助。我在类似的话题上四处寻找,似乎找不到解决方案。
发布于 2018-07-29 05:22:07
假设您的数据结构为Dict(key1: List(Dict( ...) ),key2:...)
试一试
data = {d:data[d][0] for d in data}
pd.DataFrame.from_dict(data)
https://stackoverflow.com/questions/51575033
复制相似问题