ノック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()