0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python実践データ分析100本ノック第2版でエラー

Posted at

ノック25:利用履歴データを集計しよう

でつまづき30分かかったので共有します。

uselog_customer = uselog_months.groupby("customer_id").agg(["mean", "median", "max", "min"])["count"]
uselog_customer = uselog_customer.reset_index(drop=False)
uselog_customer.head()

エラー内容

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/groupby/groupby.py in _agg_py_fallback(self, how, values, ndim, alt)
   1873         try:
-> 1874             res_values = self.grouper.agg_series(ser, alt, preserve_dtype=True)
   1875         except Exception as err:

24 frames
TypeError: Could not convert string '201804201805201806201807201808201809201810201811201812201901201902201903' to numeric

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/groupby/groupby.py in _agg_py_fallback(self, how, values, ndim, alt)
   1876             msg = f"agg function failed [how->{how},dtype->{ser.dtype}]"
   1877             # preserve the kind of exception that raised
-> 1878             raise type(err)(msg) from err
   1879 
   1880         if ser.dtype == object:

TypeError: agg function failed [how->mean,dtype->object]

["count"]の位置を手前に持ってくることでエラー回避できた。

uselog_customer = uselog_months.groupby("customer_id")["count"].agg(["mean", "median", "max", "min"])
uselog_customer = uselog_customer.reset_index(drop=False)
uselog_customer.head()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?