0

i am trying to convert json to pandas dataframe using read_json, but it is always creating extra columns instead of rows

json:

'[{"1981121": {"Summary": "Tasa"}}, {"1981123": {"Summary": "This fox only jumps on the top"}}]'

code:

pd.read_json(json,orient='index')

result:

                 0                                              1
1981121  {'Summary': 'Tasa'}                                   NaN
1981123         NaN                {'Summary': 'This fox only jumps on the top'}

i have tried different values for 'orient' arg yet it is the same

how can i get dataframe in this manner

               0         
1981121  {'Summary': 'Tasa'}                                           
1981123  {'Summary': 'This fox only jumps on the top'}

1 Answer 1

1

Pandas expects each record to be a tuple, not a dict. Here's one way to make it work:

items = [next(iter(d.items())) for d in json]
pd.DataFrame.from_items(items, orient='index', columns=['Summary'])

Then you get:

                                Summary
1981121                            Tasa
1981123  This fox only jumps on the top
Sign up to request clarification or add additional context in comments.

1 Comment

Thats exactly what i needed! Any advice on how can i improve to gain knowledge, like knowing pandas expects to be tuple?

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.