Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - JSON array to DataFrame

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?

like image 301
Darren Christopher Avatar asked May 06 '18 12:05

Darren Christopher


2 Answers

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.

like image 156
Ami Tavory Avatar answered Oct 18 '22 22:10

Ami Tavory


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
like image 36
Rakesh Avatar answered Oct 18 '22 20:10

Rakesh