TypeError Traceback (most recent call last) Cell In[1], line 20 17 df = pd.DataFrame(data) 19 # 计算各项消费的平均值 —> 20 average_consumption = df.mean() 22 # 绘制消费水平对比图 23 fig, ax = plt.subplots(figsize=(10, 6))
File /usr/local/lib/python3.10/site-packages/pandas/core/frame.py:11693, in DataFrame.mean(self, axis, skipna, numeric_only, **kwargs) 11685 @doc(make_doc(“mean”, ndim=2)) 11686 def mean( 11687 self, (…) 11691 **kwargs, 11692 ):
11693 result = super().mean(axis, skipna, numeric_only, **kwargs) 11694 if isinstance(result, Series): 11695 result = result.finalize(self, method=“mean”)
File /usr/local/lib/python3.10/site-packages/pandas/core/generic.py:12420, in NDFrame.mean(self, axis, skipna, numeric_only, **kwargs) 12413 def mean( 12414 self, 12415 axis: Axis | None = 0, (…) 12418 **kwargs, 12419 ) -> Series | float:
12420 return self._stat_function( 12421 “mean”, nanops.nanmean, axis, skipna, numeric_only, **kwargs 12422 )
File /usr/local/lib/python3.10/site-packages/pandas/core/generic.py:12377, in NDFrame._stat_function(self, name, func, axis, skipna, numeric_only, **kwargs) 12373 nv.validate_func(name, (), kwargs) 12375 validate_bool_kwarg(skipna, “skipna”, none_allowed=False)
12377 return self._reduce( 12378 func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only 12379 )
File /usr/local/lib/python3.10/site-packages/pandas/core/frame.py:11562, in DataFrame._reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds) 11558 df = df.T 11560 # After possibly _get_data and transposing, we are now in the 11561 # simple case where we can use BlockManager.reduce
11562 res = df._mgr.reduce(blk_func) 11563 out = df._constructor_from_mgr(res, axes=res.axes).iloc[0] 11564 if out_dtype is not None and out.dtype != “boolean”:
File /usr/local/lib/python3.10/site-packages/pandas/core/internals/managers.py:1500, in BlockManager.reduce(self, func) 1498 res_blocks: list[Block] = [] 1499 for blk in self.blocks: -> 1500 nbs = blk.reduce(func) 1501 res_blocks.extend(nbs) 1503 index = Index([None]) # placeholder
File /usr/local/lib/python3.10/site-packages/pandas/core/internals/blocks.py:404, in Block.reduce(self, func) 398 @final 399 def reduce(self, func) -> list[Block]: 400 # We will apply the function and reshape the result into a single-row 401 # Block with the same mgr_locs; squeezing will be done at a higher level 402 assert self.ndim == 2 –> 404 result = func(self.values) 406 if self.values.ndim == 1: 407 res_values = result
File /usr/local/lib/python3.10/site-packages/pandas/core/frame.py:11481, in DataFrame._reduce.<locals>.blk_func(values, axis) 11479 return np.array([result]) 11480 else:
11481 return op(values, axis=axis, skipna=skipna, **kwds)
File /usr/local/lib/python3.10/site-packages/pandas/core/nanops.py:147, in bottleneck_switch.call.<locals>.f(values, axis, skipna, **kwds) 145 result = alt(values, axis=axis, skipna=skipna, **kwds) 146 else: –> 147 result = alt(values, axis=axis, skipna=skipna, **kwds) 149 return result
File /usr/local/lib/python3.10/site-packages/pandas/core/nanops.py:404, in _datetimelike_compat.<locals>.new_func(values, axis, skipna, mask, **kwargs) 401 if datetimelike and mask is None: 402 mask = isna(values) –> 404 result = func(values, axis=axis, skipna=skipna, mask=mask, **kwargs) 406 if datetimelike: 407 result = _wrap_results(result, orig_values.dtype, fill_value=iNaT)
File /usr/local/lib/python3.10/site-packages/pandas/core/nanops.py:720, in nanmean(values, axis, skipna, mask) 718 count = _get_counts(values.shape, mask, axis, dtype=dtype_count) 719 the_sum = values.sum(axis, dtype=dtype_sum) –> 720 the_sum = _ensure_numeric(the_sum) 722 if axis is not None and getattr(the_sum, “ndim”, False): 723 count = cast(np.ndarray, count)
File /usr/local/lib/python3.10/site-packages/pandas/core/nanops.py:1686, in _ensure_numeric(x) 1683 inferred = lib.infer_dtype(x) 1684 if inferred in [“string”, “mixed”]: 1685 # GH#44008, GH#36703 avoid casting e.g. strings to numeric -> 1686 raise TypeError(f"Could not convert {x} to numeric") 1687 try: 1688 x = x.astype(np.complex128)
TypeError: Could not convert [‘宝安区龙岗区’] to numeric