Search code examples
pythonlistdictionarykeyscreen-scraping

pandas getting a value from a key that contains a list of dictionaries


how will i just get 'Spongebob Squarepants' from this:

[{'characters': [{'id': 'Spongebob Squarepants', 'name': 'Spongebob Squarepants'}]}]

i've tried doing

data['characters']['id']

and

data['characters']['name']

but it resulted in the error

TypeError: list indices must be integers or slices, not str

And when I saved it to a dataframe the character column becomes this whole string

[{'id': 'Spongebob Squarepants', 'name': 'Spongebob Squarepants'}]}]

Thank you!


Solution

  • data['characters'] is a list, so you'd have to do:

    data['characters'][0]['id']