将包含具有多个值的另一个词典列表的词典列表转换为dataframe

时间:2018-05-01 15:43:11

标签: python pandas dictionary dataframe

此问题是Convert list of dictionaries containing another list of dictionaries to dataframe

发布的问题的补充

我被要求在我的API调用中添加一个参数,现在输出变得有点复杂了。

输出是这样的:

insights = [ <Insights> "account_id": "1234",
                    "actions": [{'value': '5', 'action_type': 'add_to_cart', 'view': '5'}],
                    "cust_id": "xyz123",
                    "cust_name": "xyz",
}, <Insights> {
    "account_id": "1234",
    "cust_id": "pqr123",
    "cust_name": "pqr",
},  <Insights> {
    "account_id": "1234",
    "actions": [
        {'click': '8', 'value': '110', 'action_type': 'add_to_cart', 'view': '102'}, {'value': '12', 'action_type': 'purchase', 'view': '12'}
    ],
    "cust_id": "abc123",
    "cust_name": "abc",
 }
 ]

现在我想要像这样的解决方案

- account_id a2cart_view a2cart_click pur_view pur_click cust_id cust_name
- 1234                 5                                   xyz123 xyz
- 1234                                                     pqr123 pqr
- 1234               102           8        12             abc123 abc

我尝试在上面的链接中使用解决方案,但是当程序无法在其中一行中找到特定值时卡住了。

1 个答案:

答案 0 :(得分:1)

我认为通过更改我之前问题的答案,您可以实现您想要的目标。仍然首先填充nan空列表:

df['actions'][df['actions'].isnull()] = df['actions'][df['actions'].isnull()].apply(lambda x: [])

然后使用另一个参数find_action定义函数what

def find_action (list_action, action_type, what):
    for action in list_action:
        # for each action, see if the key action_type is the one wanted and what in the keys
        if action['action_type'] == action_type and what in action.keys():
            return action[what]
    # if not the right action type found, then empty
    return ''

现在,您可以将apply与两个参数一起使用:

df['a2cart_view'] = df['actions'].apply(find_action, args=(['add_to_cart','view']))
df['a2cart_click'] = df['actions'].apply(find_action, args=(['add_to_cart','click']))
df['pur_view'] = df['actions'].apply(find_action, args=(['purchase','view']))
df['pur_click'] = df['actions'].apply(find_action, args=(['purchase','click']))

并删除列actions

df = df.drop('actions',axis=1)