【发布时间】:2021-07-24 05:43:23
【问题描述】:
我有 a_list:
[{
'attributes': {
'title': 'apple',
'id': '5543',
'owner': 'tom',
}
},
{
'attributes': {
'title': 'pear',
'id': '5432',
'owner': 'suzy',
}
},
{
'attributes': {
'title': 'orange',
'id': '1234',
'owner': 'james',
}
}]
我试图将其作为一个简单的数据框返回。
通过查看其他帖子,嵌套字典让我认为我应该使用 json_normalize 并传入 record_path 的属性。
df = pd.json_normalize(a_list, record_path='attributes', meta= ['title', 'id', 'owner'])
但是,这会返回一个异常:
TypeError: {'attributes': {'title': 'apple', 'id': '5543', 'owner': 'tom'}} has non list value {'title': 'apple', 'id': '5543', 'owner': 'tom'} for path attributes. Must be list or null.
我在这里做错了什么?
【问题讨论】:
标签: python pandas dataframe dictionary