简体   繁体   English

如何从字典列表中创建几列

[英]how to create several columns from list of dictionaries

So I am trying to parse Facebook ads data using the Facebook Graph API, I was able to create and get all the data I needed, however two of the returned items are lists of dictionaries, specifically the actions field and action_value field, each of which contains the name of the action and the value of that action.所以我正在尝试使用 Facebook 图表 API 解析 Facebook 广告数据,我能够创建并获取所有数据,我需要的每个字段,但是其中两个字段项是具体返回的操作_值列表包含动作的名称和动作的值。

I have added the 2 list of dicts as is in my dataframe, however, I need to actually have each name/value in a column, the column name would be the action name from the dict, and the value would be the action value from the same dict.我在 dataframe 中添加了 2 个字典列表,但是,我实际上需要在列中包含每个名称/值,列名将是字典中的操作名称,值将是来自同一个字典。 so instead of having for example 30 columns per row, I would like to have 28 + however many dicts are within the actions or action_value lists.因此,我不想每行有 30 列,而是希望有 28 + 但是很多 dicts 在操作或 action_value 列表中。 df 与动作值和动作字典一起返回

数据集中表格中的图像 Please notice how the action_values and action columns consist of several values 'which are dicts' I would like to have each of the items within the dict to be a column of its own.请注意 action_values 和 action 列是如何由几个值“这是 dicts”组成的,我想让 dict 中的每个项目成为它自己的列。

maybe you can try pd.DataFrame.fromdict()也许你可以试试 pd.DataFrame.fromdict()

import pandas as pd

data = [{'action_type': 'dataframe', 'value': 1},
        {'action_type': 'read','value': 1}, 
        {'action_type': 'post','value': 25}]

pd.DataFrame.from_dict(data)
    action_type     value
0   dataframe        1
1   read             1
2   post            25

I got your point.我明白你的意思了。 You need to extract every kv pairs from the list of dictionaries to generate a flat one.您需要从字典列表中提取每个 kv 对以生成一个扁平的。 Here's some code:这是一些代码:

import pandas as pd


if __name__ == '__main__':
    data = [{"action_values": [{"action_type": "post", "value": 1},{"action_type": "purchase", "value": 1}],
             "actions": [{"action_type": "post_1", "value": 1}, {"action_type": "purchase", "value": 2}],
             "click": 123}]
    cleaned = []
    for i in data:
        tmp = {**i}
        tmp.pop("action_values")
        tmp.pop("actions")
        for action_values in i["action_values"]:
            for k, v in action_values.items():
                tmp[f"action_values/{k}"] = v
            for action in i["actions"]:
                for k2, v2 in action.items():
                    tmp[f"actions/{k2}"] = v2
                cleaned.append(tmp)
    df = pd.DataFrame(cleaned)
    print(df)

Thus, In the before, you have one row in df, with action_values field assigns with list of two dictionaries, actions field assign with list of two dictionaries.因此,在前面,您在 df 中有一行,action_values 字段分配有两个字典的列表,actions 字段分配有两个字典的列表。 After processing, you get 2 * 2 = 4 rows in df.处理后,您在 df 中得到2 * 2 = 4行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM