将包含另一个词典列表的词典列表转换为数据框 [英] Convert list of dictionaries containing another list of dictionaries to dataframe
本文介绍了将包含另一个词典列表的词典列表转换为数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图寻找解决方案,但无法获得1.我从python中的api获得以下输出.
I tried to look for the solution and I am unable to get 1. I have the following output from an api in python.
insights = [ <Insights> {
"account_id": "1234",
"actions": [
{
"action_type": "add_to_cart",
"value": "8"
},
{
"action_type": "purchase",
"value": "2"
}
],
"cust_id": "xyz123",
"cust_name": "xyz",
}, <Insights> {
"account_id": "1234",
"cust_id": "pqr123",
"cust_name": "pqr",
}, <Insights> {
"account_id": "1234",
"actions": [
{
"action_type": "purchase",
"value": "45"
}
],
"cust_id": "abc123",
"cust_name": "abc",
}
]
我想要这样的数据框
- account_id add_to_cart purchase cust_id cust_name
- 1234 8 2 xyz123 xyz
- 1234 pqr123 pqr
- 1234 45 abc123 abc
当我使用以下
> insights_1 = [x for x in insights]
> df = pd.DataFrame(insights_1)
我得到以下内容
- account_id actions cust_id cust_name
- 1234 [{'value': '8', 'action_type': 'add_to_cart'},{'value': '2', 'action_type': 'purchase'}] xyz123 xyz
- 1234 NaN pqr123 pqr
- 1234 [{'value': '45', 'action_type': 'purchase'}] abc123 abc
我该如何进行?
推荐答案
这是一种解决方案.
df = pd.DataFrame(insights)
parts = [pd.DataFrame({d['action_type']: d['value'] for d in x}, index=[0])
if x == x else pd.DataFrame({'add_to_cart': [np.nan], 'purchase': [np.nan]})
for x in df['actions']]
df = df.drop('actions', 1)\
.join(pd.concat(parts, axis=0, ignore_index=True))
print(df)
account_id cust_id cust_name add_to_cart purchase
0 1234 xyz123 xyz 8 2
1 1234 pqr123 pqr NaN NaN
2 1234 abc123 abc NaN 45
说明
- 利用
pandas
将字典的外部列表读入数据框. - 对于内部字典,将列表理解与字典理解结合使用.
- 通过测试列表理解力是否相等来计算
nan
值. - 连接零件并将其连接到原始数据框.
- Utilise
pandas
to read the outer list of dictionaries into a dataframe. - For the inner dictionaries, use a list comprehension together with a dictionary comprehension.
- Account for
nan
values by testing for equality within the list comprehension. - Concatenate and join the parts to the original dataframe.
说明-详细信息
这详细介绍了parts
的构造和使用:
This details the construction and use of parts
:
- 获取
df['actions']
中的每个条目;每个条目将是一个列表 字典. - 在
for
循环中逐行(即逐行)对其进行迭代. -
else
部分说如果它是np.nan
[即null],则返回nan
s的数据帧".if
部分获取字典列表,并为每行创建一个迷你数据框 . - 然后,我们使用下一部分将这些小词典连接起来,每行一个,然后将它们连接到原始数据框.
- Take each entry in
df['actions']
; each entry will be a list of dictionaries. - Iterate them one by one, i.e. by row, in a
for
loop. - The
else
part says "if it isnp.nan
[i.e. null] then return a dataframe ofnan
s". Theif
part takes the list of dictionaries and creates a mini-dataframe for each row. - We then use the next part to concatenate these mini-dictionaries, one for each row, and join them to the original dataframe.
这篇关于将包含另一个词典列表的词典列表转换为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文