1
我想使用pandas和json_normalize在python中標準化JSON數據。 下面的代碼行33工作正常。在第36行,我改變了最後一個元素,以「數字」,並得到了以下錯誤消息:json_normalize傳遞ValueError,需要區分前綴
Traceback (most recent call last):
File "/Users/Home/Downloads/JSONtoCSV/testflattening.py", line 32, in <module>
print json_normalize(data, 'items', [['address','city'], ['address','company_name'], 'number'])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/io/json/normalize.py", line 262, in json_normalize
'need distinguishing prefix ' % k)
ValueError: Conflicting metadata name number, need distinguishing prefix
我想這是因爲該項目「數量」與熊貓的內部變量衝突。所以我需要重新命名所有'數字'項目。
我試過使用下面一行28碼第一種方法 - 但我得到的錯誤信息:
ValueError: 'number' is not in list
很抱歉,但我完全新的Python - 我究竟做錯了什麼?
謝謝!
import pandas
from pandas.io.json import json_normalize
data = [{'address': {
'city': 'city A',
'company_name': 'company A'},
'amount': 998,
'items': [{'description': 'desc A1','number': 'number A1'}],
'number': 'number of A',
'service_date': {
'type': 'DEFAULT',
'date': '2015-11-18'},
'vat_option': 123},
{'address': {
'city': 'city B',
'company_name': 'company B'},
'amount': 222,
'items': [{'description': 'desc B1','number': 'number B1'},
{'description': 'desc B2','number': 'number B2'}],
'number': 'number of B',
'service_date': {
'type': 'DEFAULT',
'date': '2015-11-18'},
'vat_option': 456}
]
data[data.index("number")] = "numbr"
print data
# working
#print json_normalize(data, 'items', [['address','city'], ['address','company_name'], 'amount'])
# not working
#print json_normalize(data, 'items', [['address','city'], ['address','company_name'], 'numbr'])