How can I flatten this json with python and pandas?

python json pandas

213 观看

1回复

1 作者的声誉

I am trying with this code but doesn't work for me, I want to flat all, thanks in advance.

from pandas.io.json import json_normalize    
data = json.load(data.json)
df = json_normalize(data, [ '1.0',"email",['password']])

if i try this one,

df = json_normalize(data, [ '1.0',["password"]]), 

i only can get the paasword, but i try this one

df = json_normalize(data, [ '1.0',"email",['password']]), 

this is the error TypeError: string indices must be integers

This is the sample of my json:

{
    "1.0": [{
            "edit_distance": [
                0,
                1
            ],
            "email": "a@gmail.com",
            "password": [
                "123123123",
                "1231232"
            ]
        },
        {
            "edit_distance": [
                0,
                1
            ],
            "email": "b@c.com",
            "password": [
                "123123",
                "123123"
            ]
        }
    ]
}
作者: testeo 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

1 作者的声誉

I'm assuming you want the three keys in the list of dictionaries to be the columns. You were very close.

from pandas.io.json import json_normalize    
data = json.load(data.json)
df = json_normalize(data, ['1.0',]) # the path to your records is '1.0'
                                    # and it gives us a list of dicts/records.

Yields:

edit_distance   email       password
[0, 1]          a@gmail.com [123123123, 1231232]
[0, 1]          b@c.com     [123123, 123123]
作者: Tad Tenacious 发布者: 2017 年 12 月 27 日
32x32