jakc
jakc

Reputation: 1171

TypeError when convert list of dictionaries to a DataFrame

I have a_list:

[{
    'attributes': {
        'title': 'apple',
        'id': '5543',
        'owner': 'tom',
    }
},
{
    'attributes': {
        'title': 'pear',
        'id': '5432',
        'owner': 'suzy',
    }
},
{
    'attributes': {
        'title': 'orange',
        'id': '1234',
        'owner': 'james',
    }
}]

I am trying to return this as a simple dataframe.

From looking at other posts, the nested dictionary leads me to think I should be using json_normalize and passing in the attributes for the record_path.

df = pd.json_normalize(a_list, record_path='attributes', meta= ['title', 'id', 'owner'])

However, this returns an exception:

TypeError: {'attributes': {'title': 'apple', 'id': '5543', 'owner': 'tom'}} has non list value {'title': 'apple', 'id': '5543', 'owner': 'tom'} for path attributes. Must be list or null.

What have I done wrong here?

Upvotes: 2

Views: 61

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195408

You can use simple list comprehension:

df = pd.DataFrame([d["attributes"] for d in lst])
print(df)

Prints:

    title    id  owner
0   apple  5543    tom
1    pear  5432   suzy
2  orange  1234  james

Upvotes: 2

Related Questions