如何在 pandas 中执行LINQ ThenBy? [英] How to perform LINQ ThenBy in pandas?
本文介绍了如何在 pandas 中执行LINQ ThenBy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有简单的数据:
type age
A 4
A 4
B 4
A 5
我想得到
type age count
A 4 2
A 5 1
B 4 1
如何在熊猫中执行此类操作:df.groupby(['type'])
之后我要做什么壳?
How to perform such thing in panda: what shell I do after df.groupby(['type'])
?
推荐答案
让我们先将groupby
与'type'和'age'一起使用,然后再使用count
和reset_index
:
Let's use groupby
with 'type' and 'age', then count
and reset_index
:
df.groupby(['type','age'])['age'].count().reset_index(name='count')
输出:
type age count
0 A 4 2
1 A 5 1
2 B 4 1
这篇关于如何在 pandas 中执行LINQ ThenBy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文