Reputation: 390
I am reading a json file to get data. The json begins like this:
{
"rows": [
[
2013,
"Mazda",
"3",
"917723",
7795,
20895
],
[
2016,
"Cadillac",
"ATS",
"DD16135D",
54890,
66000
]
I am trying to get the values in a row into separate columns. I have tried doing the following:
df = pd.read_json(path, orient='values')
and got the result:
rows
0 [2013, Mazda, 3, 917723, 77...
1 [2016, Cadillac, ATS, DD161...
2 [2015, Mitsubishi, Outlander,...
3 [2016, BMW, 528I, 918058, 3...
4 [2015, Toyota, Venza, 91806...
Ultimately, I would like to have 7 columns in my dataframe, each column representing each item in a row. How can I achieve this?
Upvotes: 1
Views: 1615
Reputation: 164813
You can read the json
file into a dictionary and select the 'rows'
key:
# replace data with open('data.json')
with data as f:
df = pd.DataFrame(json.load(f)['rows'])
print(df)
0 1 2 3 4 5
0 2013 Mazda 3 917723 7795 20895
1 2016 Cadillac ATS DD16135D 54890 66000
Setup
from io import StringIO
import json
data = StringIO("""{
"rows": [
[
2013,
"Mazda",
"3",
"917723",
7795,
20895
],
[
2016,
"Cadillac",
"ATS",
"DD16135D",
54890,
66000
]]}""")
Upvotes: 2