Reputation: 315
I know there has been already several questions on this topic, but I can't find the answer.
I have a nested JSON, let's say:
data = {'items': [{'block': 1,
'log_events': [{'decoded': {'name': 'Transfer'},
'params': ['param0', 'param1', 'param2']}]},
{'block': 2,
'log_events': [{'decoded': {'name': 'SafeReceived'},
'params': ['param3', 'param4', 'param5']}]}]}
I tried:
pd.json_normalize(
data=data['items'],
record_path=['log_events'],
meta=['block']
)
But I would like to "flatten" both on the 'log_events' and on the 'params'. How can I do that ?
Upvotes: 0
Views: 40
Reputation: 862491
IIUC use:
df = pd.json_normalize(
data=data['items'],
record_path=['log_events'],
meta=['block']
).explode('params').reset_index(drop=True)
print (df)
params decoded.name block
0 param0 Transfer 1
1 param1 Transfer 1
2 param2 Transfer 1
3 param3 SafeReceived 2
4 param4 SafeReceived 2
5 param5 SafeReceived 2
Upvotes: 1