Facebook API调用在列中提供了一个字典-如何将键设置为列,将值设置为行Python

问题描述 投票:0回答:1

我正在对Facebook进行API调用,其中一个字段是“操作”,它创建了一个字典,我想将其分解成单独的DataFrame列。我使用pd.Series()将它们映射到单独的列或json.normalize()中,也遇到了一些类似的问题,但这些问题并不能完全满足我的要求。

这里是导出,然后将其放入数据框列:

[<AdsInsights> {
    "actions": [
        {
            "action_type": "landing_page_view",
            "value": "292"
        },
        {
            "action_type": "comment",
            "value": "13"
        },
        {
            "action_type": "onsite_conversion.post_save",
            "value": "6"
        },
        {
            "action_type": "link_click",
            "value": "874"
        },
        {
            "action_type": "post",
            "value": "1"
        },
        {
            "action_type": "post_reaction",
            "value": "393"
        },
        {
            "action_type": "post_engagement",
            "value": "96"
        },
        {
            "action_type": "page_engagement",
            "value": "96"
        },
        {
            "action_type": "omni_activate_app",
            "value": "5"
        },
        {
            "action_type": "omni_app_install",
            "value": "2"
        },
        {
            "action_type": "omni_add_to_cart",
            "value": "75"
        },
        {
            "action_type": "add_to_wishlist",
            "value": "14"
        },
        {
            "action_type": "omni_purchase",
            "value": "4"
        },
        {
            "action_type": "omni_search",
            "value": "12"
        },
        {
            "action_type": "omni_view_content",
            "value": "15"
        }
    ]

然后将其放入DF,但该列变为动作,并将所有这些数据放入一行,然后针对每个项目重复此数据。我在将action_type分解为列标题并将值分解为行时遇到麻烦。创建DF时,我会得到这些数据集的多行。

当我制作数据框时,它看起来像这样:

    df[ad] df[actions]
0   ad1     [{'action_type': 'landing_page_view', 'value':...
1   ad2     [{'action_type': 'landing_page_view', 'value':...
2   ad3     [{'action_type': 'landing_page_view', 'value':...

我希望得到:

    df[ad] df[landing_page_view] df[comment] ...etc
0   ad1     292                      13
1   ad2     100                      8 
2   ad3     80                       9 

我正在尝试挑选要创建列的特定列,但这不起作用:

df = all of the raw data from the API call
df = pd.DataFrame(df)
actions = df['actions']
def setcolumn(dict, key): 

    if dict.has_key(key): 
        df['key'] = 'value' 
    else: 
        print ("Not present")

setcolumn(actions, 'landing_page_view')

但是这表示系列对象没有属性has_key。

任何方向都值得赞赏!

python pandas facebook dictionary series
1个回答
0
投票

我稍微修改了您的API结果,因为不确定开头的标记是什么。

该解决方案是一个名为from_records()的简单熊猫方法,该方法将列表转换为数据框。以下是一些参考链接:

import pandas as pd

# facebook API result as dictionary ... I removed the '<AdsInsights> ' at the beginning
list_facebook_api = {
    "actions": [
        {
            "action_type": "landing_page_view",
            "value": "292"
        },
        {
            "action_type": "comment",
            "value": "13"
        },
        {
            "action_type": "onsite_conversion.post_save",
            "value": "6"
        },
        {
            "action_type": "link_click",
            "value": "874"
        },
        {
            "action_type": "post",
            "value": "1"
        },
        {
            "action_type": "post_reaction",
            "value": "393"
        },
        {
            "action_type": "post_engagement",
            "value": "96"
        },
        {
            "action_type": "page_engagement",
            "value": "96"
        },
        {
            "action_type": "omni_activate_app",
            "value": "5"
        },
        {
            "action_type": "omni_app_install",
            "value": "2"
        },
        {
            "action_type": "omni_add_to_cart",
            "value": "75"
        },
        {
            "action_type": "add_to_wishlist",
            "value": "14"
        },
        {
            "action_type": "omni_purchase",
            "value": "4"
        },
        {
            "action_type": "omni_search",
            "value": "12"
        },
        {
            "action_type": "omni_view_content",
            "value": "15"
        }
    ]
}

# create a dataframe from the list inside the dictionary
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_records.html
# https://pbpython.com/pandas-list-dict.html
# https://stackoverflow.com/questions/44563707/how-to-create-pandas-dataframe-with-index-from-the-list-of-tuples
df = pd.DataFrame.from_records(list_facebook_api['actions'])

print(df.shape)

# print(df)
df

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.