-2

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?

thangaraj1980
  • 141
  • 2
  • 11
  • 1
    Does this answer your question? [Construct pandas DataFrame from items in nested dictionary](https://stackoverflow.com/questions/13575090/construct-pandas-dataframe-from-items-in-nested-dictionary) – bonCodigo May 19 '23 at 11:33
  • No. Finally I achieved using jsonnormalize – thangaraj1980 May 20 '23 at 14:28

3 Answers3

1

After fixing the typos in your d ('Lx' instead of Lx), you can re-arange/fix your code this way :

df = pd.DataFrame.from_dict({k: v[0] for k, v in d.items()}, orient="index")

​ Output :

print(df)

    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
Timeless
  • 22,580
  • 4
  • 12
  • 30
0

For general solution create DataFrames in dictionary comprehension with concat, last remove second level of MultiIndex by DataFrame.droplevel:

df = pd.concat({k: pd.DataFrame(v) for k, v in d.items()}).droplevel(1)
print (df)
    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

If there are one element lists is possible use:

df = pd.DataFrame.from_dict({k: v[0] for k, v in d.items()}, orient='index')
print (df)
    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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

You can do like this:

df = pd.DataFrame.from_records((v[0] for v in d.values()), index=d.keys())

+--+-----+-------------+-----+-------+
|  |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    |
+--+-----+-------------+-----+-------+
ErnestBidouille
  • 1,071
  • 4
  • 16