Feb-29-2024, 04:53 PM
My guess is mpg.csv contains something that confuses pands.read_csv.
I can get the same error like this:
I can get the same error like this:
import pandas as pd df = pd.read_csv("data.csv") print(df) print(df.dtypes) print(df.groupby("model_year").mean()["mpg"])
Output: model_year mpg
0 1 l
1 2 1
2 2 2
3 3 1
4 3 2
5 3 3
model_year int64
mpg object
dtype: object
Error:Traceback (most recent call last):
File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 1871, in _agg_py_fallback
res_values = self.grouper.agg_series(ser, alt, preserve_dtype=True)
File "venv\lib\site-packages\pandas\core\groupby\ops.py", line 850, in agg_series
result = self._aggregate_series_pure_python(obj, func)
File "venv\lib\site-packages\pandas\core\groupby\ops.py", line 871, in _aggregate_series_pure_python
res = func(group)
File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 2377, in <lambda>
alt=lambda x: Series(x).mean(numeric_only=numeric_only),
File "venv\lib\site-packages\pandas\core\series.py", line 6221, in mean
return NDFrame.mean(self, axis, skipna, numeric_only, **kwargs)
File "venv\lib\site-packages\pandas\core\generic.py", line 11978, in mean
return self._stat_function(
File "venv\lib\site-packages\pandas\core\generic.py", line 11935, in _stat_function
return self._reduce(
File "venv\lib\site-packages\pandas\core\series.py", line 6129, in _reduce
return op(delegate, skipna=skipna, **kwds)
File "venv\lib\site-packages\pandas\core\nanops.py", line 147, in f
result = alt(values, axis=axis, skipna=skipna, **kwds)
File "venv\lib\site-packages\pandas\core\nanops.py", line 404, in new_func
result = func(values, axis=axis, skipna=skipna, mask=mask, **kwargs)
File "venv\lib\site-packages\pandas\core\nanops.py", line 720, in nanmean
the_sum = _ensure_numeric(the_sum)
File "venv\lib\site-packages\pandas\core\nanops.py", line 1693, in _ensure_numeric
raise TypeError(f"Could not convert string '{x}' to numeric")
TypeError: Could not convert string 'l' to numeric
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test.py", line 7, in <module>
print(df.groupby("model_year").mean()["mpg"])
File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 2375, in mean
result = self._cython_agg_general(
File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 1926, in _cython_agg_general
new_mgr = data.grouped_reduce(array_func)
File "venv\lib\site-packages\pandas\core\internals\managers.py", line 1428, in grouped_reduce
applied = sb.apply(func)
File "venv\lib\site-packages\pandas\core\internals\blocks.py", line 366, in apply
result = func(self.values, **kwargs)
File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 1923, in array_func
result = self._agg_py_fallback(how, values, ndim=data.ndim, alt=alt)
File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 1875, in _agg_py_fallback
raise type(err)(msg) from err
TypeError: agg function failed [how->mean,dtype->object]
In my example my mpg.csv has a lowercase L instead of a one. You get a slightly different error trace, but that may be due to the mpg.csv file having a slightly different error. Can you post mpg.csv?