Python - 从数据框中提取信息 (JSON)

Python - Extract information from dataframe (JSON)

我是初学者,很长一段时间没有编写任何代码:-) 我正在使用请求库从 Incapsula(云网络安全服务)检索 JSON 数据 API 获取有关网站的一些统计信息。我最终想要的是将 "type of trafic, timestamp, and number" 写入文件以创建报告。 API 响应是这样的:

{
    "res": 0,
    "res_message": "OK",
    "visits_timeseries" : [
        {
            "id":"api.stats.visits_timeseries.human",
            "name":"Human visits",
            "data":[
                [1344247200000,50],
                [1344247500000,40],
                ...
            ]
        },
        {
            "id":"api.stats.visits_timeseries.bot",
            "name":"Bot visits",
            "data":[
                [1344247200000,10],
                [1344247500000,20],
                ...
            ]
        }

我正在像这样恢复 Visit_timeseries 数据:

r = requests.post('https://my.incapsula.com/api/stats/v1', params=payload)
reply=r.json()
reply = reply['visits_timeseries']
reply = pandas.DataFrame(reply)

我以那种形式恢复数据(Unix 时间日期,访问次数):

print(reply[['name', 'data']].head())

name                                               data
0  Human visits  [[1500163200000, 39], [1499904000000, 73], [14...
1    Bot visits  [[1500163200000, 1891], [1499904000000, 1926],...

我不明白如何从数据框中提取我想要的字段,只将它们写入 excel。我需要将数据字段修改为两行(日期、值)。并且只有名称作为顶行。

很棒的是:

        Human Visit      Bot Visit
Date       Value           Value
Date       Value           Value
Date       Value           Value

感谢您的帮助!

好吧,如果有帮助的话,这是一个硬编码版本:

import pandas as pd

reply =  {
    "res": 0,
    "res_message": "OK",
    "visits_timeseries" : [
        {
            "id":"api.stats.visits_timeseries.human",
            "name":"Human visits",
            "data":[
                [1344247200000,50],
                [1344247500000,40]
            ]
        },
        {
            "id":"api.stats.visits_timeseries.bot",
            "name":"Bot visits",
            "data":[
                [1344247200000,10],
                [1344247500000,20]
            ]
        }
        ]
        }

human_data = reply['visits_timeseries'][0]['data']
bot_data = reply['visits_timeseries'][1]['data']

df_h = pd.DataFrame(human_data, columns=['Date', 'Human Visit'])
df_b = pd.DataFrame(bot_data, columns=['Date', 'Bot Visit'])
df = df_h.append(df_b, ignore_index=True).fillna(0)
df = df.groupby('Date').sum()