Issue
JSON/dict data
data = {"outer_level_key": [{
"random1": "",
"url": "",
"software": [{
"name": "a123",
"version": "some_version_a123"
}],
"memory": 72,
},
{
"random1": "",
"url": "",
"software": [{
"name": "a456",
"version": "some_version_a456"
},
{
"name": "a789",
"version": "some_version_a789"
},
{
"name": "b123",
"version": "some_version_b123"
}
],
"memory": 500
}}
I would like to print all the available data name
and version
in below format
++++++++++++++++++++++
name. | version. |
+++++++++++++++++++++++
+ a123. |some_version_a123|
+---------------------------
+ a456. | some_version_a456|
+----------------------------
Any better readability table would be better. Can you please help me ?
Solution
Here's one way to do it.
(i) Traverse data
and collect the relevant data and merge them on key using dict.setdefault
to create out
(ii) Pass out
from (i) to DataFrame
constructor.
out = {}
for outer_dict in data['outer_level_key']:
for inner_dict in outer_dict['software']:
for k, v in inner_dict.items():
out.setdefault(k, []).append(v)
df = pd.DataFrame(out)
Output:
name version
0 a123 some_version_a123
1 a456 some_version_a456
2 a789 some_version_a789
3 b123 some_version_b123
Answered By - enke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.