There is mean
, max
, median
, min
and mad
yet no mode
for groupby
objects. Is there a good reason? What if I add and issue pull-request?
Comment From: sinhrks
related to #11562. Nice to discuss how the output should be.
Comment From: jreback
yes, closing as a duplicate of that issue.
The crus is that mode is not a reducer (like sum
, mean
) and such, nor is it a straight filter, like nth
, or tail
. Its like .value_counts
which we don't directly support either via groupby
you can do this:
In [6]: df = DataFrame({'A' : [1,2,1,2], 'B' : [1,1,1,1]})
In [7]: df
Out[7]:
A B
0 1 1
1 2 1
2 1 1
3 2 1
In [8]: df.groupby('A').B.value_counts()
Out[8]:
A B
1 1 2
2 1 2
Name: B, dtype: int64
In [9]: df.groupby('A').B.apply(lambda x: x.mode())
Out[9]:
A
1 0 1
2 0 1
Name: B, dtype: int64