出现以下异常,什么意思呀
import pandas as pddf = pd.read_excel('E:/team.xlsx')
df1 = df.groupby('team').mean()
print(df1)
Traceback (most recent call last):
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\groupby.py", line 1490, in array_func
result = self.grouper._cython_operation(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\ops.py", line 959, in _cython_operation
return cy_op.cython_operation(
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\ops.py", line 657, in cython_operation
return self._cython_op_ndim_compat(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\ops.py", line 497, in _cython_op_ndim_compat
return self._call_cython_op(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\ops.py", line 541, in _call_cython_op
func = self._get_cython_function(self.kind, self.how, values.dtype, is_numeric)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\ops.py", line 173, in _get_cython_function
raise NotImplementedError(
NotImplementedError: function is not implemented for this dtype:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python\Python311\Lib\site-packages\pandas\core\nanops.py", line 1692, in _ensure_numeric
x = float(x)
^^^^^^^^
ValueError: could not convert string to float: 'AckLfieOscarJoshuaHenryLucasArthurReggie1TobyDylanHugo0CalebNathanBlakeStanleyTylerAaron'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python\Python311\Lib\site-packages\pandas\core\nanops.py", line 1696, in _ensure_numeric
x = complex(x)
^^^^^^^^^^
ValueError: complex() arg is a malformed string
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\K\PycharmProjects\pythonProject\777.py", line 4, in <module>
df1 = df.groupby('team').mean()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\groupby.py", line 1855, in mean
result = self._cython_agg_general(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\groupby.py", line 1507, in _cython_agg_general
new_mgr = data.grouped_reduce(array_func)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\internals\managers.py", line 1503, in grouped_reduce
applied = sb.apply(func)
^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\internals\blocks.py", line 329, in apply
result = func(self.values, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\groupby.py", line 1503, in array_func
result = self._agg_py_fallback(values, ndim=data.ndim, alt=alt)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\groupby.py", line 1457, in _agg_py_fallback
res_values = self.grouper.agg_series(ser, alt, preserve_dtype=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\ops.py", line 994, in agg_series
result = self._aggregate_series_pure_python(obj, func)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\ops.py", line 1015, in _aggregate_series_pure_python
res = func(group)
^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\groupby\groupby.py", line 1857, in <lambda>
alt=lambda x: Series(x).mean(numeric_only=numeric_only),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\generic.py", line 11556, in mean
return NDFrame.mean(self, axis, skipna, numeric_only, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\generic.py", line 11201, in mean
return self._stat_function(
^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\generic.py", line 11158, in _stat_function
return self._reduce(
^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\series.py", line 4670, in _reduce
return op(delegate, skipna=skipna, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\nanops.py", line 96, in _f
return f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\nanops.py", line 158, in f
result = alt(values, axis=axis, skipna=skipna, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\nanops.py", line 421, in new_func
result = func(values, axis=axis, skipna=skipna, mask=mask, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\nanops.py", line 727, in nanmean
the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_sum))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\pandas\core\nanops.py", line 1699, in _ensure_numeric
raise TypeError(f"Could not convert {x} to numeric") from err
TypeError: Could not convert AckLfieOscarJoshuaHenryLucasArthurReggie1TobyDylanHugo0CalebNathanBlakeStanleyTylerAaron to numeric
出现了无法将字符串转换为浮点数的错误,某些列包含非数值型的数据,导致计算平均值时出错
将'column1','column2','column3'替换为实际包含数值型数据的列名
以下是修改后的代码示例:
import pandas as pd
df = pd.read_excel('E:/team.xlsx')
numeric_cols = ['column1', 'column2', 'column3']# 替换为实际的数值型列名
df = df.apply(pd.to_numeric, errors='coerce')
df1 = df.groupby('team').mean()
print(df1)
页:
[1]