【问题标题】:DataError: No numeric types to aggregateDataError:没有要聚合的数字类型
【发布时间】:2021-09-22 03:39:06
【问题描述】:

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

【问题讨论】:

  • 请勿在您的问题上粘贴图片。数据和错误消息都必须包含为格式化文本。

标签: python pandas dataframe pandas-groupby numeric


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    为了您的准确答案

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

    【讨论】:

      猜你喜欢
      • 2019-06-23
      • 1970-01-01
      • 2020-09-20
      • 2021-08-07
      • 1970-01-01
      • 2017-01-06
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多