Home > Software design >  how to create several columns from list of dictionaries
how to create several columns from list of dictionaries

Time:09-05

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.

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. 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. df returned with the action value and actions dicts

CodePudding user response:

maybe you can try 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

CodePudding user response:

I got your point. You need to extract every k v pairs from the list of dictionaries to generate a flat one. 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. After processing, you get 2 * 2 = 4 rows in df.

  • Related