在Python中,如何将包含dict的嵌套列表转换为数据帧中的额外列
我从API的dict中收到信息
{'orders':
[
{ 'orderId': '2838168630',
'dateTimeOrderPlaced': '2020-01-22T18:37:29+01:00',
'orderItems': [{ 'orderItemId': 'BFC0000361764421',
'ean': '234234234234234',
'cancelRequest': False,
'quantity': 1}
]},
{ 'orderId': '2708182540',
'dateTimeOrderPlaced': '2020-01-22T17:45:36+01:00',
'orderItems': [{ 'orderItemId': 'BFC0000361749496',
'ean': '234234234234234',
'cancelRequest': False,
'quantity': 3}
]},
{ 'orderId': '2490844970',
'dateTimeOrderPlaced': '2019-08-17T14:21:46+02:00',
'orderItems': [{ 'orderItemId': 'BFC0000287505870',
'ean': '234234234234234',
'cancelRequest': True,
'quantity': 1}
]}
通过这样做,我成功地将其转化为一个简单的数据帧:
pd.DataFrame(recieved_data.get('orders'))
输出:
orderId date oderItems
1 1-12 [{orderItemId: 'dfs13', 'ean': '34234'}]
2 etc.
...
我想要这样的
orderId date oderItemId ean
1 1-12 dfs13 34234
2 etc.
...
我已经尝试用Iloc挑出orderItems列,然后将其转换为一个列表,这样我就可以再次尝试提取值。然而,我最终还是得到了一个列表,我需要从中提取另一个列表,其中包含dict
希望对你有用
干杯
通过综合这个问题的答案,我达到了我的最终目标。我认为:
另外,我没有使用.concat(),而是使用.join()基于现有索引连接两个表
为了清楚起见,您正在从API接收一个json,因此您可以尝试使用函数
json_normalize
。 试试这个:因此,输出为:
相关问题 更多 >
编程相关推荐