简体   繁体   中英

Load json file with string into pandas dataframe in python

I have an example json dataset as:

['[{"id":"123","product":"dell","date":"2019-01-01","sales":5,"created_at":"2019-01-26 15:00:00"}, {"id":"124","product":"apple","date":"2019-01-02","sales":7,"created_at":"2019-01-27 15:00:00"}]']

I would like to create a pandas dataframe from this json data but when i use the json_normalize method i get AttributeError: 'str' object has no attribute 'values'.

The expected output should be like this:

id   product  date        sales   created_at
123.  dell.   2019-01-01.  5.     2019-01-26 15:00:00
124.  apple.  2019-01-02.  7.     2019-01-27 15:00:00

thats when i get the output which is a list of string

If the result of json.load... is like

a = ['[{"id":"123","product":"dell","date":"2019-01-01","sales":5,"created_at":"2019-01-26 15:00:00"}, {"id":"124","product":"apple","date":"2019-01-02","sales":7,"created_at":"2019-01-27 15:00:00"}]']

Then do it again...

>>> b = json.loads(a[0])
>>> b
[{'id': '123', 'product': 'dell', 'date': '2019-01-01', 'sales': 5, 'created_at': '2019-01-26 15:00:00'}, {'id': '124', 'product': 'apple', 'date': '2019-01-02', 'sales': 7, 'created_at': '2019-01-27 15:00:00'}]
>>> pd.DataFrame(b)
    id product        date  sales           created_at
0  123    dell  2019-01-01      5  2019-01-26 15:00:00
1  124   apple  2019-01-02      7  2019-01-27 15:00:00
>>>

Unfortunately you will not have the luxury of knowing in advance that you need to do this. You would need to inspect the data first. Unless you are lucky enough to have the specifications of the thing that is making these jsons.

import json
import pandas as pd
x = ['[{"id":"123","product":"dell","date":"2019-01-01","sales":5,"created_at":"2019-01-26 15:00:00"}, {"id":"124","product":"apple","date":"2019-01-02","sales":7,"created_at":"2019-01-27 15:00:00"}]']
# Take 0th element of list
x = x[0]
info = json.loads(x)
df = pd.json_normalize(info)
df

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM