根据另一个值从DataFrame中提取一个值

时间:2020-10-30 10:46:05

标签: python pandas dataframe facebook-graph-api

我正在使用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)吗?

1 个答案:

答案 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'])