我有一个嵌套字典的示例,如下所示:
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')
}
}]
是type(data)
一个列表。
我想把它放在数据帧格式上,这样预期的输出是这样的:
primaryEnergy_wellToTank primaryEnergy_tankToWheel carbonDioxide_wellToTank carbonDioxide_tankToWheel
13412 56939 0.902 3.963
我尝试了 pd.Dataframe 函数的一些转换:
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() ]))
但到目前为止,结果并不真正成功。
怎么可能做到这一点?
以下是我使用时遇到的错误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'
serialize_object
我可以通过使用该功能来解决问题。