import pandas as pd
dict = {'Company': ['COMP1', 'COMP1', 'COMP2', 'COMP2', 'COMP3', 'COMP3'],
'Months': ['MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG'],
'Salary': [2000, 1500, 3000, 5000, 2500, 3500]}
df = pd.DataFrame(dict)
print(df)
df.groupby('Company').mean()
我正在使用3.12版本,我认为该版本有错误,然后我切换到3.10,但问题仍然存在。请各位程序员帮忙!!!
File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\generic.py", line 12370, in _stat_function
return self._reduce( File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\series.py", line 6437, in _reduce
return op(delegate, skipna=skipna, **kwds) File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\nanops.py", line 147, in f
result = alt(values, axis=axis, skipna=skipna, **kwds) File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\nanops.py", line 404, in new_func
result = func(values, axis=axis, skipna=skipna, mask=mask, **kwargs) File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\nanops.py", line 720, in nanmean
the_sum = _ensure_numeric(the_sum) File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\nanops.py", line 1701, in _ensure_numeric
raise TypeError(f"Could not convert string '{x}' to numeric") TypeError: Could not convert string 'MARAPR' to numeric
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "D:\pyprojects\pythonProject1\pandas&numpy.py", line 245, in <module>
df.groupby('Company').mean() File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\groupby\groupby.py", line 2452, in mean
result = self._cython_agg_general( File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\groupby\groupby.py", line 1998, in _cython_agg_general
new_mgr = data.grouped_reduce(array_func) File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\managers.py", line 1470, in grouped_reduce
applied = sb.apply(func) File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\blocks.py", line 393, in apply
result = func(self.values, **kwargs) File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\groupby\groupby.py", line 1995, in array_func
result = self._agg_py_fallback(how, values, ndim=data.ndim, alt=alt) File "C:\Users\respu\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\groupby\groupby.py", line 1946, in _agg_py_fallback
raise type(err)(msg) from err TypeError: agg function failed [how->mean,dtype->object]