【发布时间】:2019-05-28 12:56:16
【问题描述】:
我正在制作一个预测模型来预测收入并尝试从数据框中解析这个 'cast' 值,因为它不是 list 或 字典
x['cast']
输出是
0 [{'cast_id': 4, 'character': 'Lou', 'credit_id...
1 [{'cast_id': 1, 'character': 'Mia Thermopolis'...
2 [{'cast_id': 5, 'character': 'Andrew Neimann',...
3 [{'cast_id': 1, 'character': 'Vidya Bagchi', '...
4 [{'cast_id': 3, 'character': 'Chun-soo', 'cred...
5 [{'cast_id': 6, 'character': 'Pinocchio (voice...
6 [{'cast_id': 23, 'character': 'Clyde', 'credit...
7 [{'cast_id': 2, 'character': 'Himself', 'credi...
8 [{'cast_id': 1, 'character': 'Long John Silver...
9 [{'cast_id': 24, 'character': 'Jonathan Steinb...
Name: cast, dtype: object
我需要一个列表中的所有 'character' 值。 但是当我尝试
x['cast'][0]['character']
它会抛出这个错误
TypeError: string indices must be integers
请帮帮我。
【问题讨论】:
-
我刚刚搜了一下,它是一个json文件,但我仍然无法将它加载为'json.load(x['cast'])'
-
type(x.at[0, 'cast'])显示什么?长话短说:是字符串还是列表? -
@SergeBallesta - 检查第一句话 -
I am making a predictive model to predict revenue and trying to parse this 'cast' value from the data frame as it is not a list or a dict -
@jezrael:您可能是对的,但在这种情况下,我认为应该在 输入数据帧之前修复它。
-
恕我直言,您应该想知道此列值的来源,以及如何避免该问题。
标签: json python-3.x pandas csv