1
私は、pandasとjson_normalizeを使用してPythonでJSONデータを正規化したいと考えています。 以下のコードの33行目は問題ありません。私はアイテムの数は「パンダの内部変数と競合しているためであると考え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
:行36で、私は「数」に最後の要素を変更し、次のエラーメッセージが表示されます。だから私はすべての '番号'アイテムの名前を変更する必要があります。
私は最初のアプローチでは、以下の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'])