我正在使用Facebook Ads API,我已为其中一个广告系列提取了广告系列数据。如果我有此数据框:
[<Insights> {
"actions": [
{
"action_type": "custom_event_abc",
"value": 50
},
{
"action_type": "custom_event_def",
"value": 42
},]
我该如何获取custom_event_def
的价值?
在更广泛的结果中,我首先在有效的代码中使用了(df.loc[0]['actions'][1]['value'])
,但是我的问题是custom_event_abc
并不总是出现,因此custom_event_def
的位置可以更改;意味着我的解决方案仅在某些时间有效。
可以使用对value
的引用来抽出action_type
(42)吗?
答案 0 :(得分:0)
这将首先创建一个包含“动作”内容的字典actions
,遍历它的所有值以找到custom_event_def
,然后打印相应的值
actions = df.loc[0]['actions']
for i, elem in enumerate(actions):
if elem['action_type'] == "custom_event_def":
print(actions[i]['value'])