我是一个初学者,很长一段时间我没有编码任何东西:-)我正在使用请求库从Incapsula(云网络安全服务)API中检索
JSON数据,以获得有关网站的一些统计数据.我最终想要的是将“trafic,timestamp和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()