5

I have a dataframe with a Date column, I group the data by year and I can compute mean and median. But how to compute the mode? Here is the error I get:

>>> np.random.seed(0)
>>> rng = pd.date_range('2010-01-01', periods=10, freq='2M')
>>> df = pd.DataFrame({ 'Date': rng, 'Val': np.random.random_integers(0,100,size=10) })
>>> df
        Date  Val
0 2010-01-31   44
1 2010-03-31   47
2 2010-05-31   64
3 2010-07-31   67
4 2010-09-30   67
5 2010-11-30    9
6 2011-01-31   83
7 2011-03-31   21
8 2011-05-31   36
9 2011-07-31   87
>>> df.groupby(pd.Grouper(key='Date',freq='A')).mean()
                  Val
Date                 
2010-12-31  49.666667
2011-12-31  56.750000
>>> df.groupby(pd.Grouper(key='Date',freq='A')).median()
             Val
Date            
2010-12-31  55.5
2011-12-31  59.5
>>> df.groupby(pd.Grouper(key='Date',freq='A')).mode()

Traceback (most recent call last):
  File "<pyshell#109>", line 1, in <module>
    df.groupby(pd.Grouper(key='Date',freq='A')).mode()
  File "C:\Python27\lib\site-packages\pandas\core\groupby.py", line 554, in __getattr__
    return self._make_wrapper(attr)
  File "C:\Python27\lib\site-packages\pandas\core\groupby.py", line 571, in _make_wrapper
    raise AttributeError(msg)
AttributeError: Cannot access callable attribute 'mode' of 'DataFrameGroupBy' objects, try using the 'apply' method
1

3 Answers 3

2
  • use np.unique with the return_counts parameter.
  • use the argmax on the counts array to get value from unique array.
  • use np.apply_along_axis for a custom function mode

def mode(a):
    u, c = np.unique(a, return_counts=True)
    return u[c.argmax()]

df.groupby(pd.Grouper(key='Date',freq='A')).Val.apply(mode)

Date
2010-12-31    67
2011-12-31    21
Freq: A-DEC, Name: Val, dtype: int64
1

mode isn't a built in function that's automatically compatible with pandas groupby objects. You could use the scipy.stats module. This feels a little clunky, though.

from scipy import stats

df.groupby(pd.Grouper(key='Date',freq='A')).apply(stats.mode)

Alternatively, you could use the value_counts() function and take the first index value returned. This is the route I would go.

df.groupby(pd.Grouper(key='Date', freq='A')).value_counts()[0].index.values[0]
0

mode is problematic as others have mentioned, however a DataFrameGroupby object can be applied a trivial lambda function, just as the AttributeError suggests using (and contains no ugly slicing or anything else):

df.groupby(grouping_column)[[i for i in pivotable_columns]].apply(lambda x: x.mode())]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.