python - 通过解析 json 列创建新列

标签 python json pandas

我有一个带有一个 json (Vals) 列的数据框:

                            Identity                                                                                                      Vals
                  2fc9d38d-0fe4-c7be       {"$id":"2","Address":"22.44","Location":{"Code":"TN"},"Asset":false,"Roles":["A"],"Type":"webfile"}
                        abd77d57ac29 {"$id":"3","Address":"40.1","Location":{"Code":"SS"},"Asset":false,"Roles":["Attacker"],"Type":"webfile"}
                           c7be-4a37                  {"$id":"4","AppId":11161,"SaasId":11161,"Name":"Office 365","InstanceId":0,"Type":"app"}
              916a-8051-8fd1721385ae                              {"$id":"3","Address":"213.85","Asset":false,"Roles":["tm"],"Type":"webfile"}
                   8051-8fd1721385ae                     {"$id":"4","Address":"198.137","Asset":false,"Roles":["Contextual"],"Type":"webfile"}
                        8fd1721385ae                             {"$id":"5","AppId":26324,"sId":26324,"Name":"MB","InstanceId":0,"Type":"app"}
                       58a51721385ae                      {"$id":"6","Address":".225.0","Asset":false,"Roles":["Contextual"],"Type":"webfile"}
964fb17e-a352-dbd4-d5b7-374172d811aa                                   {"$id":"2","Name":"AD561-SA","DisplayName":"AD561-SA","Type":"account"}

我想为“Type”创建一个新列来保存值 - “webfile”、“app”等。运行以下代码:

df_test["new_col"]=df_test['Vals'].apply(lambda x: x['Type'] if 'Type' in x else None)

但出现错误

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

有人可以帮忙吗?

最佳答案

由于您的 Vals 列包含 JSON 字符串,因此您必须在提取 Type 字段之前先进行解码:

import json

df_test['Type'] = pd.json_normalize(df_test['Vals'].apply(json.loads))['Type']

输出:

>>> df_test[['Identity', 'Type']]
                               Identity     Type
0                    2fc9d38d-0fe4-c7be  webfile
1                          abd77d57ac29  webfile
2                             c7be-4a37      app
3                916a-8051-8fd1721385ae  webfile
4                     8051-8fd1721385ae  webfile
5                          8fd1721385ae      app
6                         58a51721385ae  webfile
7  964fb17e-a352-dbd4-d5b7-374172d811aa  account

关于python - 通过解析 json 列创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75663339/

相关文章:

python - 修复在 python 中用 BS4 提取的损坏的 html 表

python - 如何从自定义分布编写抽样算法?

json - R 格式化 JSON 输出的策略

javascript - 过滤一个 json 并用另一个 json 进行响应

python - Pandas 数据框 : join items in range based on their geo coordinates (longitude and latitude)

python - 从 pandas DataReader 导入数据时,x 轴日期未显示在 matplotlib 上

python - 如何在没有错误的情况下构建 py2exe?

python - 运行脚本以在终端中调用命令

python - 如何从该输入字符串中提取数字

带有 key :object 的 JavaScript json