DataError: No numeric types to aggregate

DataError: No numeric types to aggregate

我将按性别描述男性和女性群体,以可视化每个值的流失率(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

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

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

为您精准解答

df_data.Churn = df_data.Churn.astype(int)