首页 > 解决方案 > 从 json 格式的 csv 数据框单元格中查找特定值

问题描述

所以我有一个带有“cast”列的电影数据框。某部电影的第一个“演员”列如下所示:

[{'cast_id': 42, 'character': 'Ted the Bellhop', 'id': 3129, 'name': 'Tim Roth'},
 {'cast_id': 31, 'character': 'Man', 'id': 3131, 'name': 'Antonio Banderas'}, 
 {'cast_id': 29, 'character': 'Angela','id': 3130, 'name': 'Jennifer Beals'}]

我的问题是 - 我如何使用pandas.apply这个值来整理这个看起来是 JSON 格式的值。我想通过查找所有“字符”来对这个值进行排序,所以我希望我的结果看起来像:

['Ted the Bellhop', 'Man', 'Angela']

标签: pythonpandas

解决方案


您也可以只使用地图功能来做到这一点。

cast = [
    {
        "cast_id":42,
        "character":"Ted the Bellhop",
        "id":3129,
        "name":"Tim Roth"
    },
    {
        "cast_id":31,
        "character":"Man",
        "id":3131,
        "name":"Antonio Banderas"
    },
    {
        "cast_id":29,
        "character":"Angela",
        "id":3130,
        "name":"Jennifer Beals"
    }
]
output = list(map(lambda x: x['character'], cast))

推荐阅读