在matplotlib中绘制条形图时,没有数字类型来聚合错误



当我运行以下代码时,我得到了一个" No numeric types to aggregate "错误。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
x_labels = ['Female', 'Male']
genderincomeTM1 = round(TM1.groupby('Gender')['Income'].mean())
genderincomeTM2 = round(TM2.groupby('Gender')['Income'].mean())
genderincomeTM3 = round(TM3.groupby('Gender')['Income'].mean())
genderTM1 = genderincomeTM1.index
genderTM2 = genderincomeTM2.index
genderTM3 = genderincomeTM3.index
x = np.arange(len(x_labels))
plt.figure(figsize=(12,8))
width = 0.35 
fig, ax = plt.subplots()
bar1 = ax.bar(x - 0.3, genderincomeTM1, width=0.2, label='TM1')
bar2 = ax.bar(x, genderincomeTM2, width=0.2, label='TM2')
bar3 = ax.bar(x + 0.3, genderincomeTM3, width=0.2, label='TM3')
ax.set_title('Average Income by Product Model', fontsize = 18)
ax.set_ylabel('Sales', fontsize = 12)
ax.set_xticks(x)
ax.set_xticklabels(x_labels)
ax.set_ylim(bottom = 0, top = 90000)
ax.legend(loc=(1.02,0.4), borderaxespad=0, fontsize = 12)
def autolabel(bars):
for each in bars:
height = each.get_height()
ax.annotate('{}'.format(height),
xy=(each.get_x() + each.get_width() / 2, height),
xytext=(0, 2),  # 2 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(bar1)
autolabel(bar2)
autolabel(bar3)
DataError                                 Traceback (most recent call last)
<ipython-input-24-fb1aa4ae1242> in <module>
1 x_labels = ['Female', 'Male']
2 
----> 3 genderincomeTM1 = round(TM1.groupby('Gender')['Income'].mean())
4 genderincomeTM2 = round(TM2.groupby('Gender')['Income'].mean())
5 genderincomeTM3 = round(TM3.groupby('Gender')['Income'].mean())
~anaconda3libsite-packagespandascoregroupbygroupby.py in mean(self, *args, **kwargs)
1223         """
1224         nv.validate_groupby_func("mean", args, kwargs, ["numeric_only"])
-> 1225         return self._cython_agg_general(
1226             "mean", alt=lambda x, axis: Series(x).mean(**kwargs), **kwargs
1227         )
~anaconda3libsite-packagespandascoregroupbygroupby.py in _cython_agg_general(self, how, alt, numeric_only, min_count)
906 
907         if len(output) == 0:
--> 908             raise DataError("No numeric types to aggregate")
909 
910         return self._wrap_aggregated_output(output)
DataError: No numeric types to aggregate

我已经删除了所有空行,并使用is_numeric_dtype检查了"收入"列。我还将该列转换为int。

from pandas.api.types import is_numeric_dtype
is_numeric_dtype(df['Income'])
>True
df['Income'] = df['Income'].astype(int)
df.info()
><class 'pandas.core.frame.DataFrame'>
>Int64Index: 180 entries, 0 to 182
>Data columns (total 10 columns):
> #   Column         Non-Null Count  Dtype 
>---  ------         --------------  ----- 
> 0   Product        180 non-null    object
> 1   Branch         180 non-null    object
> 2   Age            180 non-null    object
> 3   Gender         180 non-null    object
> 4   Education      180 non-null    object
> 5   MaritalStatus  180 non-null    object
> 6   Usage          180 non-null    object
> 7   Fitness        180 non-null    object
> 8   Income         180 non-null    int32 

我很困惑为什么验证后没有收入的数字类型。它指的是"性别"吗?我应该如何解决这个错误?

我可能无法回答您的问题,因为您没有提供再现错误的数据框架。也许你可以从运行这段代码开始:

import numpy as np
import pandas as pd
TM1 = pd.DataFrame({'Gender':['M','F','M','F'],'Income':['10','20','30','40']})
TM1['Income'] = TM1.Income.astype(int)
TM1.groupby('Gender')['Income'].mean().round()

由于收入最初是以字符串形式给出的,因此在将其转换为整数之前无法计算平均值。

最新更新