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?
There are two problems in your question:
to_csv
on a list.=
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With