代码之家  ›  专栏  ›  技术社区  ›  RustyShackleford

如何迭代另一列中的嵌套字段以基于另一个值创建新列?

  •  0
  • RustyShackleford  · 技术社区  · 7 年前

    col1     nested-filed
    1        [{nested_data}]
    

    嵌套字段中的数据如下所示:

    [{'field': 1, 'timestamp': 1511404149332, 'changed-timestamp': 0, 'identities': [{'type': 'leadid', 'value': '123-456', 'timestamp': 1488815181110}, {'type': 'ID', 'value': '0987654321', 'timestamp': 1489691285116}, {'type': 'EMAIL', 'value': '1@1', 'timestamp': 1488815179334, 'is': True}]}]
    

    按排我想退出 email ID

    col1     nested-filed          email           ID
    1        [{nested_data}]       1@1.com         0987654321
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   meW    7 年前

    你可以试试这个-

    import ast
    df.nested_filed = df.nested_filed.apply(lambda x: ast.literal_eval(x))
    
    # Store in a new column named email
    df['email'] = df.nested_filed.apply(lambda x: x[2]['value'])
    
    # Store in a new column named ID
    df['ID'] = df.nested_filed.apply(lambda x: x[1]['value'])