pandas 从包含字典列表的键中获取值

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

我如何才能从中得到 'Spongebob Squarepants':

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

我试过

data['characters']['id']

data['characters']['name']

但它导致了错误

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

当我将它保存到数据框时,字符列变成了整个字符串

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

谢谢!

data['characters'] 是一个列表,所以你必须这样做:

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