Pandas groupby.mean() gives me "no number type aggregation"-but .sum() works?
The content comes from Stack Overflow and is translated and used in accordance with the CC BY-SA 3.0 license agreement
The content comes from Stack Overflow and is translated and used in accordance with the CC BY-SA 3.0 license agreement
- Answer (1)
- Follow (0)
- View (6145)
I use Pandas in Python 3. For some reason, I can group and then sum() my data frame:
full_data.groupby('polarity')['pos'].sum()
polarity
both 1.842
neg 5.241
neu 496.026
pos 245.105
Name: pos, dtype: float64
When I swapped out the average of the amount , but I get this error:
DataError: No numeric types to aggregate
Do you know why this happens? I have confirmed that each item in the'pos' column is a floating point number, and run the following code without output:
for i in full_data.loc[:,‘pos’]:
if type(i) != float:
print(‘not a float’)
thank you for your help!
sum
And the mean
behavior is different. Consider these examples:
In [2]: df = pd.DataFrame({
‘key’: [‘a’, ‘b’, ‘b’], ‘val’: [1.2, 2.3, 3.4]})
In [3]: df.groupby(‘key’).val.sum()
Out[3]:
key
a 1.2
b 5.7
Name: val, dtype: float64
In [4]: df.groupby(‘key’).val.mean()
Out[4]:
key
a 1.20
b 2.85
Name: val, dtype: float64
In [7]: df.dtypes
Out[7]:
key object
val float64
dtype: object
Now, if I change the val
column so that it is the dtype of the object:
In [8]: df[‘val’] = df.val.astype(object)
In [9]: df.groupby(‘key’).val.mean()
–––––––––––––––––––––––––––––––––––––-
DataError Traceback (most recent call last)
<ipython-input-9-b46b3a9673d0> in <module>()
––> 1 df.groupby(‘key’).val.mean()
~\Miniconda3\lib\site-packages\pandas\core\groupby\groupby.py in mean(self, *args, kwargs)
1304 nv.validate_groupby_func(‘mean’, args, kwargs, [‘numeric_only’])
1305 try:
-> 1306 return self._cython_agg_general(‘mean’, kwargs)
1307 except GroupByError:
1308 raise
~\Miniconda3\lib\site-packages\pandas\core\groupby\groupby.py in _cython_agg_general(self, how, alt, numeric_only, min_c
ount)
1054
1055 if len(output) == 0:
-> 1056 raise DataError(‘No numeric types to aggregate’)
1057
1058 return self._wrap_aggregated_output(output, names)
DataError: No numeric types to aggregate
In [10]: df.groupby(‘key’).val.sum()
Out[10]:
key
a 1.2
b 5.7
Name: val, dtype: float64
Note that mean
this column no longer applies
The dtype of a column has nothing to do with the dtype of a single cell, for example:
In [12]: isinstance(df.val[0], float)
Out[12]: True
So please check your column dtype and convert it to number.
Now why the design mean
and sum
behavior are different, because it sum
should allow operations on non-numerical data, for example str
, only require and make sense for that data type.
In [14]: df[‘val’] = [‘z’, ‘y’, ‘x’]
In [15]: df.groupby(‘key’).val.sum()
Out[15]:
key
a z
b yx
Name: val, dtype: object
Obviously, it mean
doesn't make any sense str
. So there is an extra try-except
block for sum
data that makes it non-numerical.