【问题标题】:Turn List of Dictionaries or Tuples into DataFrame. Actions - Column, Value - rows将字典或元组列表转换为 DataFrame。操作 - 列,值 - 行
【发布时间】:2020-02-13 08:54:45
【问题描述】:

我正在对 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】:

    我稍微编辑了你的 API 结果,因为我不确定开头的标签是干什么用的。

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

    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
    

    【讨论】:

    • 你好,开头的标签是我相信的类名。我必须运行一个类来获取数据,这就是它的导出方式。无论如何,当我尝试您的代码时,内核由于某种原因而死机。另外,由于我在操作列中的每行都获得了这些列表之一,所以无论如何都可以将其作为列来执行?因此,着陆页视图将是列标题,然后 292 将是第一个值,其他行的所有其他着陆页值将列在其下方。这有意义吗?有可能吗?谢谢。
    • 另外,当我将数据导出设置为变量时,该变量被视为游标。还有一件事 - 有许多其他列与这些数据一起导出,仅供参考。
    • 这是一篇关于如何删除列标题的堆栈溢出文章。 stackoverflow.com/questions/36688022/…
    • 你好@pk2019 - 我不确定这与这个主题有什么关系。谢谢
    猜你喜欢
    • 2018-01-01
    • 1970-01-01
    • 2016-03-01
    • 2013-04-01
    • 1970-01-01
    • 2010-09-20
    • 2017-06-07
    相关资源
    最近更新 更多