【问题标题】:Add array into json for each row of a dataframe为数据框的每一行将数组添加到 json 中
【发布时间】:2023-02-16 23:22:28
【问题描述】:

我想为数据框的每一行填充以下 json 数组

JSON数组

"techniques": [
        {
            "techniqueID": "",
            "tactic": "",
            "color": "",
            "comment": "",
            "enabled": true,
            "metadata": [],
            "links": [],
            "showSubtechniques": false
        }
    ]

数据框

techniqueID  Value    color                tactic
0        T1078     13  #74c476       Defense-Evasion
1        T1078     13  #74c476        Initial-Access
2        T1078     13  #74c476           Persistence
3        T1078     13  #74c476  Privilege-Escalation
4        T1110      5  #74c476     Credential-Access
5        T1070      3  #a1d99b       Defense-Evasion
6        T1059      3  #a1d99b             Execution
7        T1114      3  #a1d99b            Collection
8        T1098      3  #a1d99b           Persistenc

我试图将 json 转换为数据框并将两者结合起来,但这不会为每一行创建一个数组。

【问题讨论】:

    标签: json python-3.x pandas dataframe


    【解决方案1】:

    看来你在找to_dict('records')

    res_dict = {
        'techniques': df.to_dict('records')
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多