I am getting output web api response as below.
d = { 'L1': [{'att_1': 10, 'att_2': 'whatever',
'att_4': 214, 'att_5': 'YES'}],
'L2': [{'att_1': 11, 'att_2': 'whatever News',
'att_4': 230, 'att_5': 'another'}],
'L3': [{'att_1': 12, 'att_2': 'whatever says',
'att_4': 213, 'att_5': 'YES'}]}
from above multilevel json/dict i need pandas data frame as below.
att_1 att_2 att_4 att_5
L1 10 whatever 214 YES
L2 11 whatever News 230 another
L3 12 whatever says 213 YES
I tried as below
for k, v in d.items():
print(k)
print(v)
pd.DataFrame.from_dict({k:(v,orient='index') for k, v in d.items()}axis=1)
I am getting error. could you pls provide the solution?
3
Answers
For general solution create DataFrames in dictionary comprehension with
concat
, last remove second level of MultiIndex byDataFrame.droplevel
:If there are one element lists is possible use:
After fixing the typos in your
d
('Lx'
instead ofLx
), you can re-arange/fix your code this way :
Output :
You can do like this: