[英]How to group a list of dict into sub-lists using pandas?
输入是类似的
[
{"name": "person 1", "age": 20, "type": "student"},
{"name": "person 2", "age": 19, "type": "worker"},
{"name": "person 3", "age": 30, "type": "student"},
{"name": "person 4", "age": 25, "type": "worker"},
{"name": "person 5", "age": 17, "type": "student"}
]
当按“类型”字段分组时,应该是所需的输出
[
[
{"name": "person 1", "age": 20, "type": "student"},
{"name": "person 3", "age": 30, "type": "student"},
{"name": "person 5", "age": 17, "type": "student"}
],
[
{"name": "person 2", "age": 19, "type": "worker"},
{"name": "person 4", "age": 25, "type": "worker"}
]
]
我有以下代码用itertools来做
from itertools import groupby
input = [
{"name": "person 1", "age": 20, "type": "student"},
{"name": "person 2", "age": 19, "type": "worker"},
{"name": "person 3", "age": 30, "type": "student"},
{"name": "person 4", "age": 25, "type": "worker"},
{"name": "person 5", "age": 17, "type": "student"}
]
input.sort(key=lambda x: x["type"])
output = [list(v) for k, v in groupby(input, key=lambda x: x["type"])]
这正确地给出了结果。 然而,对于更大量的数据,我认为使用pandas应该更有效,但现在看来我无法弄清楚如何使用pandas完成上述操作。 我现在的代码有点工作,但我认为它根本没有效率。
import pandas as pd
input = [
{"name": "person 1", "age": 20, "type": "student"},
{"name": "person 2", "age": 19, "type": "worker"},
{"name": "person 3", "age": 30, "type": "student"},
{"name": "person 4", "age": 25, "type": "worker"},
{"name": "person 5", "age": 17, "type": "student"}
]
indexes = [list(v) for k, v in pd.DataFrame(input).groupby(["type"]).groups.items()]
output = [[input[y] for y in x] for x in indexes]
我很确定上面的代码是使用pandas groupby功能的一种非常错误的方法,所以任何有关如何正确执行此操作的帮助? 谢谢。
您可以使用GroupBy.apply
和to_dict
执行此to_dict
:
pd.DataFrame(input).groupby('type').apply(lambda x: x.to_dict('r')).to_list()
稍快一点,
pd.DataFrame(input).groupby('type').apply(
pd.DataFrame.to_dict, orient='r').tolist()
# [[{'age': 20, 'name': 'person 1', 'type': 'student'},
# {'age': 30, 'name': 'person 3', 'type': 'student'},
# {'age': 17, 'name': 'person 5', 'type': 'student'}],
# [{'age': 19, 'name': 'person 2', 'type': 'worker'},
# {'age': 25, 'name': 'person 4', 'type': 'worker'}]]
我将要做的
l1=[[y.iloc[0].to_dict() for z in y.iterrows()] for _ , y in pd.DataFrame(input).groupby('type')]
Out[254]:
[[{'age': 20, 'name': 'person 1', 'type': 'student'},
{'age': 20, 'name': 'person 1', 'type': 'student'},
{'age': 20, 'name': 'person 1', 'type': 'student'}],
[{'age': 19, 'name': 'person 2', 'type': 'worker'},
{'age': 19, 'name': 'person 2', 'type': 'worker'}]]
而且如果只需要与值匹配键,您可以使用itertuples
进行检查
l=[list(y.itertuples()) for _ , y in pd.DataFrame(input).groupby('type')]
Out[256]:
[[Pandas(Index=0, age=20, name='person 1', type='student'),
Pandas(Index=2, age=30, name='person 3', type='student'),
Pandas(Index=4, age=17, name='person 5', type='student')],
[Pandas(Index=1, age=19, name='person 2', type='worker'),
Pandas(Index=3, age=25, name='person 4', type='worker')]]
相比
l[0][0].age
Out[263]: 20
l1[0][0]['age']
Out[264]: 20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.