use*_*360 3 dictionary dataframe python-3.x pandas
我有一个带字典的嵌套列表.以下只是列表的第一个元素
{'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'}
Run Code Online (Sandbox Code Playgroud)
我想将数据规范化为数据帧.我把代码编写为:json_normalize(d).但我收到以下错误:
-------------------------------------------------- ------------------------- KeyError Traceback(最近一次调用last)in()----> 1 df = json_normalize(data)
json_normalize中的/anaconda3/lib/python3.6/site-packages/pandas/io/json/normalize.py(data,record_path,meta,meta_prefix,record_prefix,errors,sep)201#TODO:处理作为列表的记录值,至少错误202#合理 - > 203数据= nested_to_record(data,sep = sep)204返回DataFrame(数据)205 elif not isinstance(record_path,list):
/anaconda3/lib/python3.6/site-packages/pandas/io/json/normalize.py in nested_to_record(ds,prefix,sep,level)86 else: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 in nested_to_record(ds,prefix,sep,level)82 new_d [newkey] = v 83如果v为None:#emp键如果值为None ---> 84 new_d.pop(k)85则继续86 else:
KeyError:'地址'
我明白,因为在None中的地址,代码给了我错误.但我不知道如何解决它.在这方面的任何帮助将受到高度赞赏.提前致谢.(请注意,数据是虚拟数据)
字典格式错误.首先,您有以下行:
'holder_name': 'name user,
Run Code Online (Sandbox Code Playgroud)
其中值'name user不是有效字符串,因为它没有被右边的单引号字符括起来.
其次,在您的代码中,您有两个列表元素,即两个字典,每个字典都以字母开头{'id': ...,而不是声明的单个元素.
修复'holder_name两个字典中的值并使其成为两个成员列表后,您可以继续使用json_normalize,您将得到如下所示的输出(在stdout中打印):
amount authorization card.address.city card.address.country_code ... operation_type order_id status transaction_type 0
418.0 None xxx xx ... xx ashdgjasdfhk fgh asd 1
418.0 None xxx xx ... xx ashdgjasdfhk fgh asd
[2 rows x 42 columns]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
409 次 |
| 最近记录: |