[英]How do I group the rows of a pandas data frame by a value of a column?
How do I group the rows of a pandas data frame by a value of a column? 如何按列的值对pandas数据帧的行进行分组?
Lets say we have a data frame called df: 假设我们有一个名为df的数据框:
A B C
1 1a 1b
1 1c 1d
1 1e 1f
2 2a 2b
2 2c 2d
3 3a 3b
3 3c 3d
I'd like to use groupby to create the following : 我想使用groupby创建以下内容:
1: {[1a, 1b],
[1c, 1d],
[1e, 1f]}
2: {[2a,2b],
[2c, 2d]}
3: {[3a,3b],
[3c. 3d]}
I do realize .loc is an option. 我确实意识到.loc是一个选择。 But it is super slow for the super-large dataset I'm working on. 但对于我正在研究的超大型数据集来说,这是非常缓慢的。 Which is why I thought turning it in a dictionary of lists may be better. 这就是为什么我认为把它放在列表字典中可能会更好。
Thanks. 谢谢。
It seems you need: 看来你需要:
df = df.groupby('A')['B','C'].apply(lambda x: x.values.tolist()).to_dict()
print (df)
{1: [['1a', '1b'], ['1c', '1d'], ['1e', '1f']],
2: [['2a', '2b'], ['2c', '2d']],
3: [['3a', '3b'], ['3c', '3d']]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.