使用 Pandas 将 JSON 转换为 Table

Convert JSON to Table with Pandas

我开始研究 python,结果是 JSON 格式时我遇到了一个问题。

我的JSON是这样的:

[{'eventid': '30', 'clock': '1602601344', 'name': 'Zabbix server has been restarted (uptime < 10m)', 'severity': '2', 'hosts': [{'hostid': '10084', 'name': 'Zabbix server'}], 'acknowledges': []}, {'eventid': '42', 'clock': '1603128324', 'name': 'Zabbix server has been restarted (uptime < 10m)', 'severity': '2', 'hosts': [{'hostid': '10084', 'name': 'Zabbix server'}], 'acknowledges': []}, {'eventid': '44', 'clock': '1603138524', 'name': 'Zabbix server has been restarted (uptime < 10m)', 'severity': '2', 'hosts': [{'hostid': '10084', 'name': 'Zabbix server'}], 'acknowledges': []}]

我尝试将结果转换为带有 json_normalize 的 table,但我的结果是: Table after json_normalize

我看到 json_normalize 文档中的示例,但我不明白如何将“名称”放在“主机”列上。

我的函数是:

def get_event_get(zbx_srv, token):
    try: 
        json_event_get = {
            "jsonrpc": "2.0",
            "method": "event.get",
            "params": {
                "selectHosts": ["name"],
                "output": ["eventid", "clock", "name", "severity"],
                "select_acknowledges": "extend",
                "sortfield": ["clock"],
                "filter": {
                    "value": ["1"],
                    "object": ["0"]
                },
            },
            "auth": token,
            "id": 1
        }
        event_get_request = requests.post(zbx_srv, json=json_event_get)
        event_get = event_get_request.json()['result']
        print(event_get)
        table_event = json_normalize(event_get)
        print(table_event)
        return event_get
    except Exception as e:
        print("Erro: %s" %(e))

谁能帮我理解一下?

试试这个,data 是您在上面发布的 json 口述。

df = pd.json_normalize(data,
                       record_path=['hosts'],
                       meta=['eventid', 'clock', 'name', 'severity'],
                       meta_prefix='_')
print(df)



hostid           name _eventid      _clock                                            _name _severity
0  10084  Zabbix server       30  1602601344  Zabbix server has been restarted (uptime < 10m)         2
1  10084  Zabbix server       42  1603128324  Zabbix server has been restarted (uptime < 10m)         2
2  10084  Zabbix server       44  1603138524  Zabbix server has been restarted (uptime < 10m)         2

或者您可以使用 record_prefix 代替:

df = pd.json_normalize(data,
                       record_path=['hosts'],
                       meta=['eventid', 'clock', 'name', 'severity'],
                       record_prefix='hosts_')
print(df)




hosts_hostid     hosts_name eventid       clock                                             name severity
0        10084  Zabbix server      30  1602601344  Zabbix server has been restarted (uptime < 10m)        2
1        10084  Zabbix server      42  1603128324  Zabbix server has been restarted (uptime < 10m)        2
2        10084  Zabbix server      44  1603138524  Zabbix server has been restarted (uptime < 10m)        2