如何展平熊猫数据框多列中的字典列表
Posted
技术标签:
【中文标题】如何展平熊猫数据框多列中的字典列表【英文标题】:How to flatten list of dictionaries in multiple columns of pandas dataframe 【发布时间】:2021-08-19 18:06:57 【问题描述】:我有一个数据框,每条记录都存储一个字典列表,如下所示:
row prodect_id recommend_info
0 XQ002 ["recommend_key":"XXX567","recommend_point":50,
"recommend_key":"XXX236","recommend_point":20,
"recommend_key":"XXX090","recommend_point":35]
1 XQ003 ["recommend_key":"XXX089","recommend_point":30,
"recommend_key":"XXX567","recommend_point":20]
我想展平字典列表,使其看起来像这样
row prodect_id recommend_info_recommend_key recommend_info_recommend_point
0 XQ002 XXX567 50
1 XQ002 XXX236 20
2 XQ002 XXX090 35
3 XQ003 XXX089 30
4 XQ003 XXX567 20
我知道如何仅将一个字典列表转换为数据框。 像这样:
d = ["recommend_key":"XXX089","recommend_point":30,
"recommend_key":"XXX567","recommend_point":20]
df = pd.DataFrame(d)
row recommend_key recommend_point
0 XXX089 30
1 XXX567 20
但是当有一列存储字典列表,或者有多列存储字典列表时,我不知道如何对数据框执行此操作
row col_a col_b col_c
0 B001 ["a":"b","a":"c"] ["y":11,"a":"c"]
1 D009 ["c":"o","g":"c"] ["y":11,"a":"c","l":"c"]
2 G068 ["c":"b","a":"c"] ["a":56,"d":"c"]
3 C004 ["d":"a","b":"c"] ["c":22,"a":"c","b":"c"]
4 F011 ["h":"u","d":"c"] ["h":27,"d":"c"]
【问题讨论】:
如果您喜欢我的回答并认为它有用,请将其标记为已接受 没问题!感谢您的帮助 【参考方案1】:试试:
pd.concat([df.explode('recommend_info').drop(['recommend_info'], axis=1),
df.explode('recommend_info')['recommend_info'].apply(pd.Series)],
axis=1)
你可以对每一列一遍又一遍地做同样的事情
这是一个例子:
>>> df = pd.DataFrame('a': [[3: 4, 5: 6, 3:8, 5: 1],
... [3:2, 5:4, 3: 8, 5: 10]],
... 'b': ['X', "Y"])
>>> df
a b
0 [3: 4, 5: 6, 3: 8, 5: 1] X
1 [3: 2, 5: 4, 3: 8, 5: 10] Y
>>> df = pd.concat([df.explode('a').drop(['a'], axis=1),
... df.explode('a')['a'].apply(pd.Series)],
... axis=1)
>>> df
b 3 5
0 X 4 6
0 X 8 1
1 Y 2 4
1 Y 8 10
【讨论】:
感谢您的回答里卡多!它也适用于非结构化的字典列表吗?以上是关于如何展平熊猫数据框多列中的字典列表的主要内容,如果未能解决你的问题,请参考以下文章