4

I get JSON data from an API service, and I would like to use a DataFrame to then output the data into CSV.

So, I am trying to convert a list of dictionaries, with about 100.000 dictionaries with about 100 key value pairs, nested up to 4 levels deep, into a Pandas DataFrame.

I am using the following code, but it is painfully slow:

try:
    # Convert each JSON data event to a Pandas DataFrame
    df_i = []
    for d in data:
        df_i.append( json_normalize(d) )

    # Concatenate all DataFrames into a single one
    df = concat(df_i, axis=0)

except AttributeError:
    print "Error: Expected a list of dictionaries to parse JSON data"

Does anyone know of a better and faster way to do this?

1

2 Answers 2

4

There's a whole section in the io docs on reading json (as strings or files) directly using pd.read_json.

You ought to be able to do something like:

pd.concat((pd.read_json(d) for d in data), axis=0)

This will often be much faster than creating a temporary dict.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Given that the json format I have is nested, it was not parsed correctly by read_json(), giving me a TypeError. However, I used your form with json_normalize():df = concat((json_normalize(d) for d in data), axis=0)
3

The following example founded in pandas.io.json.json_normalize documentation (link: https://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.io.json.json_normalize.html):

>>> from pandas.io.json import json_normalize
>>> data = [{'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk'}},
        {'name': {'given': 'Mose', 'family': 'Regner'}},
        {'id': 2, 'name': 'Faye Raker'}]
>>> json_normalize(data)
   id        name name.family name.first name.given name.last
0  1.0         NaN         NaN     Coleen        NaN      Volk
1  NaN         NaN      Regner        NaN       Mose       NaN
2  2.0  Faye Raker         NaN        NaN        NaN       NaN

Comments

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.