Python - JSON array to DataFrame

14,373

Solution 1

Use df = pd.DataFrame(YourList)

Ex:

import pandas as pd

d = [
    {
        "foo":1
    },
    {
        "foo":2
    }
]

df = pd.DataFrame(d)
print(df)

Output:

   foo
0    1
1    2

Solution 2

There are two problems in your question:

  1. It called to_csv on a list.
  2. The JSON was illegal, as it contained = signs instead of :

This works by me:

import json
import pandas as pd

>>> pd.DataFrame(json.loads("""[
    {
        "foo": 1
    },
    {
        "foo": 2
    }
]"""))

   foo
0    1
1    2

You can also call read_json directly.

Share:
14,373
Darren Christopher
Author by

Darren Christopher

I enjoy writing code in python to run some analysis on top of data. Some of the data science-y works I worked on: Clustering for concept mining in forum data (https://doi.org/10.1007/978-3-030-29908-8_32) Learning to rank approach (AdaRank) to automate spectator camera movement (https://doi.org/10.1007/978-981-13-6661-1_12) Horizon scanning project Scanning the trends on a given fields by applying text mining on job site data

Updated on June 05, 2022

Comments

  • Darren Christopher
    Darren Christopher almost 2 years

    I have this following JSON array.

    [
        {
            "foo"=1
        },
        {
            "foo"=2
        },
        ...
    ]
    

    I would like to convert it to DataFrame object using pd.read_json() command like below.

    df = pd.read_json(my_json) #my_json is JSON array above
    

    However, I got the error, since my_json is a list/array of json. The error is ValueError: Invalid file path or buffer object type: <class 'list'>.

    Besides iterating through the list, is there any efficient way to extract/convert the JSON to DataFrame object?