Home > Enterprise >  max aggregation not supported for characters or strings in pandas groupby
max aggregation not supported for characters or strings in pandas groupby

Time:07-29

Below is the dataframe

    Employee_id  REGIONAL_GROUPING
0   5035496      WEST I
1   5035496      WEST I
2   5035496      TN II

My expected output is below

    Employee_id      max_REGIONAL_GROUPING   min_REGIONAL_GROUPING                       
0    5035496         WEST I                    TN II    

I'm trying to groupby on Employee_id and trying to get max and min of REGIONAL_GROUPING. Below is the code i've written

df.groupby('Employee_id').agg({'REGIONAL_GROUPING': ['min', 'max']})

It's returning an error like below

File ~\Miniconda3\lib\site-packages\pandas\core\nanops.py:1043, in _nanminmax.<locals>.reduction(values, axis, skipna, mask)
   1041         result = np.nan
   1042 else:
-> 1043     result = getattr(values, meth)(axis)
   1045 result = _maybe_null_out(result, axis, mask, values.shape)
   1046 return result

File ~\Miniconda3\lib\site-packages\numpy\core\_methods.py:44, in _amin(a, axis, out, keepdims, initial, where)
     42 def _amin(a, axis=None, out=None, keepdims=False,
     43           initial=_NoValue, where=True):
---> 44     return umr_minimum(a, axis, None, out, keepdims, initial, where)

TypeError: '<=' not supported between instances of 'str' and 'float'

CodePudding user response:

It is working for me - pandas version - 1.4.2


df.groupby(df.Employee_id).agg({"REGIONAL_GROUPING": ["min", "max"]})

result

enter image description here

  • Related