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

Pulling a value from DataFrame based on another value

我正在玩 Facebook 广告 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 并不总是出现,因此 [=11= 的位置]可以换;这意味着我的解决方案仅在某些时候有效。

可以使用对 action_type 的引用来提取 value (42) 吗?

这将首先创建一个包含“actions”内容的字典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'])