I have a sample of a nested Dictionary that looks like this:
data = [{
'resultInfo': {
'load': None,
'unload': {
'weight': 59.0,
'unit': 'ton',
'tonsPerTeu': None,
'tonsPerFeu': None,
'freightId': None,
'showEmissionsAtResponse': True
},
'location': 'zip:63937',
'freightId': None,
'emissionPercentage': 1.0,
'directDistance': 767.71
},
'emissions': {
'primaryEnergy': {
'rail': None,
'sea': None,
'air': None,
'inlandWaterways': None,
'road': {
'_value_1': Decimal('70351.631210000000'),
'wellToTank': Decimal('13412'),
'tankToWheel': Decimal('56939')
},
'logisticsite': None,
'transfer': None,
'unit': 'MegaJoule'
},
'carbonDioxide': {
'rail': None,
'sea': None,
'air': None,
'inlandWaterways': None,
'road': {
'_value_1': Decimal('4.866239643000'),
'wellToTank': Decimal('0.902'),
'tankToWheel': Decimal('3.963')
}
}]
The type(data) is a list.
I want to have it on a dataframe format so that expected output is this:
primaryEnergy_wellToTank primaryEnergy_tankToWheel carbonDioxide_wellToTank carbonDioxide_tankToWheel
13412 56939 0.902 3.963
I tried some transformation from the pd.Dataframe function:
df = pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in mydict.items() ]))df = pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in mydict.items() ]))
but the result is not really successful so far.
How this could be done ?
Below is the error i am getting when i use df = pd.json_normalize(data)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\zeep\xsd\valueobjects.py in __getattribute__(self, key)
142 try:
--> 143 return self.__values__[key]
144 except KeyError:
KeyError: 'values'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-180-cc2694b5448e> in <module>
----> 1 df = pd.json_normalize(result.result)
~\AppData\Roaming\Python\Python37\site-packages\pandas\io\json\_normalize.py in _json_normalize(data, record_path, meta, meta_prefix, record_prefix, errors, sep, max_level)
272
273 if record_path is None:
--> 274 if any([isinstance(x, dict) for x in y.values()] for y in data):
275 # naive normalization, this is idempotent for flat records
276 # and potentially will inflate the data considerably for
~\AppData\Roaming\Python\Python37\site-packages\pandas\io\json\_normalize.py in <genexpr>(.0)
272
273 if record_path is None:
--> 274 if any([isinstance(x, dict) for x in y.values()] for y in data):
275 # naive normalization, this is idempotent for flat records
276 # and potentially will inflate the data considerably for
~\AppData\Local\Continuum\anaconda3\lib\site-packages\zeep\xsd\valueobjects.py in __getattribute__(self, key)
144 except KeyError:
145 raise AttributeError(
--> 146 "%s instance has no attribute '%s'" % (self.__class__.__name__, key)
147 )
148
AttributeError: DistributionLoadResult instance has no attribute 'values'
- I could solve the issue by using the
serialize_objectfunction.