【发布时间】:2019-02-28 03:10:44
【问题描述】:
我有一个带有字典的嵌套列表。以下只是列表的第一个元素
{'id': 'abcde',
'authorization': None,
'operation_type': 'xx',
'method': 'card',
'transaction_type': 'asd',
'card': {'type': 'dd',
'brand': 'vv',
'address': {'line1': 'xxxxxxx',
'line2': '',
'line3': '',
'state': 'xx',
'city': 'xxx',
'postal_code': '12345',
'country_code': 'xx'},
'card_number': '123456XXXXXX7890',
'holder_name': 'name user,
'expiration_year': '20',
'expiration_month': '02',
'allows_charges': True,
'allows_payouts': True,
'bank_name': 'abc bank',
'bank_code': '000'},
'status': 'fgh',
'conciliated': True,
'creation_date': '2018-09-23T23:58:17-05:00',
'operation_date': '2018-09-23T23:58:17-05:00',
'description': 'asdmdefdsa',
'error_message': 'sdaskjflj',
'order_id': 'ashdgjasdfhk',
'amount': 418.0,
'customer': {'name': 'abc',
'last_name': 'xyz',
'email': 'abcdef@hotmail.com',
'phone_number': '12345678',
'address': None,
'creation_date': '2018-09-23T23:58:18-05:00',
'external_id': None,
'clabe': None},
'fee': {'amount': 0.56, 'tax': 0.91, 'currency': 'XXX'},
'currency': 'XXX'},
{'id': 'abcde',
'authorization': None,
'operation_type': 'xx',
'method': 'card',
'transaction_type': 'asd',
'card': {'type': 'dd',
'brand': 'vv',
'address': {'line1': 'xxxxxxx',
'line2': '',
'line3': '',
'state': 'xx',
'city': 'xxx',
'postal_code': '12345',
'country_code': 'xx'},
'card_number': '123456XXXXXX7890',
'holder_name': 'name user,
'expiration_year': '20',
'expiration_month': '02',
'allows_charges': True,
'allows_payouts': True,
'bank_name': 'abc bank',
'bank_code': '000'},
'status': 'fgh',
'conciliated': True,
'creation_date': '2018-09-23T23:58:17-05:00',
'operation_date': '2018-09-23T23:58:17-05:00',
'description': 'asdmdefdsa',
'error_message': 'sdaskjflj',
'order_id': 'ashdgjasdfhk',
'amount': 418.0,
'customer': {'name': 'abc',
'last_name': 'xyz',
'email': 'abcdef@hotmail.com',
'phone_number': '12345678',
'address': None,
'creation_date': '2018-09-23T23:58:18-05:00',
'external_id': None,
'clabe': None},
'fee': {'amount': 0.56, 'tax': 0.91, 'currency': 'XXX'},
'currency': 'XXX'}
我想将数据标准化为数据框。我将代码编写为:json_normalize(d)。但我收到以下错误:
----------------------------------- ---------------------------- KeyError Traceback(最近一次调用 最后)在() ----> 1 df = json_normalize(数据)
/anaconda3/lib/python3.6/site-packages/pandas/io/json/normalize.py json_normalize(数据,记录路径,元,元前缀,记录前缀, 错误,九月) 201 # TODO: 处理列表的记录值,至少错误 202#合理 --> 203 数据=nested_to_record(数据,sep=sep) 204 返回数据帧(数据) 205 elif not isinstance(record_path, list):
/anaconda3/lib/python3.6/site-packages/pandas/io/json/normalize.py nested_to_record(ds,前缀,sep,级别) 86 其他: 87 v = new_d.pop(k) ---> 88 new_d.update(nested_to_record(v, newkey, sep, level + 1)) 89 new_ds.append(new_d) 90
/anaconda3/lib/python3.6/site-packages/pandas/io/json/normalize.py nested_to_record(ds,前缀,sep,级别) 82 new_d[新键] = v 83 if v is None: # 如果值为None,则弹出键 ---> 84 new_d.pop(k) 85 继续 86 其他:
KeyError: '地址'
我明白,因为地址没有,代码给了我错误。但我不知道如何解决它。在这方面的任何帮助将不胜感激。提前致谢。 (请注意数据为虚拟数据)
【问题讨论】:
标签: python-3.x pandas dictionary dataframe