DataError:没有要聚合的数字类型

时间:2021-07-13 04:10:50

标签: python pandas dataframe pandas-groupby numeric

我将按性别描述男性和女性组,以可视化每个值的流失率(1-保留率)。

我的输出

df_data.groupby(by='gender')['Churn'].mean()



error
---------------------------------------------------------------------------
DataError                                 Traceback (most recent call last)
<ipython-input-46-75992efc6958> in <module>()
----> 1 df_data.groupby(by='gender')['Churn'].mean()

1 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/groupby/groupby.py in mean(self, numeric_only)
   1396             "mean",
   1397             alt=lambda x, axis: Series(x).mean(numeric_only=numeric_only),
-> 1398             numeric_only=numeric_only,
   1399         )
   1400 

/usr/local/lib/python3.7/dist-packages/pandas/core/groupby/groupby.py in _cython_agg_general(self, how, alt, numeric_only, min_count)
   1051 
   1052         if len(output) == 0:
-> 1053             raise DataError("No numeric types to aggregate")
   1054 
   1055         return self._wrap_aggregated_output(output, index=self.grouper.result_index)

DataError: No numeric types to aggregate

2 个答案:

答案 0 :(得分:3)

您的所有列,即使是那些看起来像数字的列,也是字符串。在应用 .astype(int) 之前,您必须使用 .mean() 将“数字”列转换为数字列。例如:

df.tenure = df.tenure.astype(int)

答案 1 :(得分:2)

为了您的准确答案

df_data.Churn = df_data.Churn.astype(int)
相关问题