问题描述
我正在使用Facebook Python SDK查询Insights端点,并且很难做出响应,因此我开始使用Python和后来的熊猫.我打了以下电话:
I'm querying an Insights endpoint with the Facebook Python SDK and have a hard time making the response I get work with Python and, subsequently pandas. I do the following call:
account = AdAccount('act_id')
params = {
'fields': [Insights.Field.impressions,Insights.Field.clicks,Insights.Field.actions,Insights.Field.spend],
'breakdowns': [Insights.Breakdown.hourly_stats_aggregated_by_advertiser_time_zone],
'time_range': {
'since': 'start',
'until': 'end',
},
'action_attribution_windows': ['7d_click'],
}
result = account.get_insights(params=params)
print (result)
返回数据的方式如下:
[<Insights> {
"actions": [
{
"7d_click": 600,
"action_type": "custom_event_xyz",
"value": 50
},
{
"7d_click": 600,
....
}
],
"clicks": 1500,
"date_start": "start",
"date_stop": "end",
"hourly_stats_aggregated_by_advertiser_time_zone": "00:00:00 - 00:59:59",
"impressions": "60000",
"spend": 60
}, <Insights> {
....
]
在将不包含actiondata的数据放入pandas DataFrame时,我无法正确地对action数据进行展平,以使聚合级别保持一致(即,"actions"键作为列标题).在线检查并在Stackoverflow上进行检查后,用python加载json并进行相应处理,再用pandas读取都是无效的.
While putting the data excluding the actionsdata into a pandas DataFrame, I do not manage to properly flatten the actions data so that the aggregation level is consistent (i.e. the "actions" keys as column headers). Having checked online and also on Stackoverflow, loading the json with python and processing it accordingly then as well as reading it with pandas are both options that do not work.
总结我看不到如何优雅地挖掘响应的更深层嵌套部分,并使内容轻松地与其余部分兼容.
Summing up I don't see how I can elegantly mine the deeper nested parts of the response and easily make the contents compatible with the rest.
推荐答案
您刚刚遇到了与我一样的各种问题. get_insights返回的数据类似于json,但并非如此.您可以知道它是什么类型,只需使用type(result).它是facebookads.objects.EdgeIterator.您可以通过使用result = [x表示结果中的x]来更改其类型,现在'结果'的类型为列表!!!然后,您可以使用pandas做任何您想做的事情,pandas.DataFrame(result).
you just met the vary same problem as i did. The get_insights returns data is like json, but it is not. You can know what type it is just use type(result). It is facebookads.objects.EdgeIterator. You can change it's type by using result=[x for x in result],now the type of the 'result' is list!!! And then you can use pandas to do whatever you want, pandas.DataFrame(result).
这篇关于如何从Python SDK解析嵌套的FB API响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!