0

So I have a piece of JSON code and I want to turn it into a DataFrame, however I am quite new to DataFrames so I am a bit stuck. Any help would be appreciated :)

So this is my code:

data = response.json()

data_pretty = json.dumps(data, sort_keys=True, indent=4)
data_frame = pd.DataFrame(data)

# Pretty print
print(data_pretty)
print(data_frame)

This is the output:

{
    "status": "OK",
    "users": [
        {
            "email": "[email protected]",
            "first_name": "Raf",
            "id": "24959",
            "last_name": "Rasenberg"
        },
        {
            "email": "[email protected]",
            "first_name": "Raf",
            "id": "25795",
            "last_name": "Rasenberg"
        }
    ]
}
  status                                              users
0     OK  {'id': '24959', 'email': '[email protected]', ...
1     OK  {'id': '25795', 'email': 'raf.rasenberg@gmail....

As you can see it needs some additional tweaking, I only want to show the columns 'email', 'first_name', 'id' and 'last_name'. Can someone help me out?

2
  • do you need something more complex than pd.DataFrame(data.get('users'))? Commented Mar 24, 2019 at 19:48
  • Cool that was the solution :) Commented Mar 24, 2019 at 19:49

1 Answer 1

1

You only need to select list of rows:

pd.DataFrame(data.get('users'))
Sign up to request clarification or add additional context in comments.

1 Comment

alternatively pd.io.json.json_normalize(data, record_path='users')

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.