Python - Retrieving information from a dataframe (JSON)

I’m starting, and I haven’t written anything for a long time :-) I use the request library to get JSON data from the Cloud web security API encapsules to get some statistics about the website. In the end, I want to write "type trafic, timestamp and number "to the report file. The API response looks something like this:

{
    "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],
                ...
            ]
        }

I am recovering Visit_timeseries data as follows:

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

I recover data in this form (date in unix time, number of visits):

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

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

I do not understand how to extract the fields that I want from a dataframe, so that I can only write them in excel. I need to change the data field to two lines (date, value). And only the name as the top lines.

Which would be great:

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

Thank you for your help!

+4
1

, - , :

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()
+1

Source: https://habr.com/ru/post/1681699/


All Articles