0

I have the following JSON from a string and read it via json.load.
I would like to normalize its contents into a pandas data frame. But instead of creating a column for each "node" in the JSON graph, I want it to create a row for each depth=1 node

import pandas as pd

pd.json_normalize({'a': {'i': 1, 'm': 'msg'}, 'b': {'i': 5, 'm': 'msg2'}} )

Output:

   a.i  a.m b.i b.m
0   1   msg 5   msg2

I want this instead:

    i   m
a   1   msg 
b   5   msg2

Do you know how to achieve that?

Thank you very much!

1 Answer 1

1

Use the orient='index' parameter

data = {'a': {'i': 1, 'm': 'msg'}, 'b': {'i': 5, 'm': 'msg2'}}
pd.DataFrame.from_dict(data=data, orient='index')

Output

   i     m
a  1   msg
b  5  msg2
Sign up to request clarification or add additional context in comments.

1 Comment

You're a saint! Thank You!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.