熊猫数据透视表错误DataError:没有要聚合的数字类型

时间:2019-12-20 09:23:16

标签: python pandas pivot-table

我刚刚开始学习python熊猫

需要为销售数据创建一个简单的数据透视表

来自https://www.contextures.com/xlSampleData01.html

的数据集示例

以下的前5条记录

OrderDate地区代表项目单位单位总成本 0 6/1/18东琼斯铅笔95 1.99 189.05 1 23/1/18中央Kivell活页夹50 19.99 999.50 2 9/2/18中央怡和铅笔36 4.99 179.64 3 26/2/18中央G笔27 19.99 539.73 4 15/3/18西索维诺铅笔56 2.99 167.44

代码如下

import pandas as pd
import numpy as np
report = pd.pivot_table(df, 
index = ("Region", "Rep"), 
columns = ("Item"),
aggfunc = (np.mean),
values = ("Total"),
margins = True)

我收到以下错误

  

DataError跟踪(最近一次调用   最后)         5 aggfunc =(np.mean),         6个值=(“总计”),   ----> 7个边距=真)

     

〜/ anaconda3 / envs / sn / lib / python3.7 / site-packages / pandas / core / reshape / pivot.py   在ivot_table中(数据,值,索引,列,agfunc,fill_value,   边距,dropna,margins_name,已观察到)        94        95分组= data.groupby(键,观察=观察)   ---> 96标记= grouped.agg(aggfunc)        97如果dropna和isinstance(agged,ABCDataFrame)和len(agged.columns):        98 agged = agged.dropna(how =“ all”)

     

〜/ anaconda3 / envs / sn / lib / python3.7 / site-packages / pandas / core / groupby / generic.py   总计(self,arg,* args,** kwargs)1453
  @Appender(_shared_docs [“ aggregate”])1454 def聚合(自己,   arg = None,* args,** kwargs):   -> 1455 return super()。aggregate(arg,* args,** kwargs)1456 1457 agg =聚合

     

〜/ anaconda3 / envs / sn / lib / python3.7 / site-packages / pandas / core / groupby / generic.py   总计(self,func,* args,** kwargs)       227 func = _maybe_mangle_lambdas(func)       228   -> 229结果,如何= self._aggregate(func,_level = _level,* args,** kwargs)       230如果没有:       231返回结果

     

〜/ anaconda3 / envs / sn / lib / python3.7 / site-packages / pandas / core / base.py在   _aggregate(self,arg,* args,** kwargs)       第568章(f)       569如果f而不是args而不是kwargs:   -> 570 return getattr(self,f)(),无       571       572#来电者可以做出反应

     

〜/ anaconda3 / envs / sn / lib / python3.7 / site-packages / pandas / core / groupby / groupby.py   平均值(self,* args,** kwargs)1203试试:1204
  返回self._cython_agg_general(   -> 1205“平均值”,alt = lambda x,轴:Series(x).mean(** kwargs),** kwargs 1206)1207
  除了GroupByError:

     

〜/ anaconda3 / envs / sn / lib / python3.7 / site-packages / pandas / core / groupby / generic.py   在_cython_agg_general中(自己,如何,alt,仅数字,最小计数)       126 def _cython_agg_general(自己,方式,alt =无,numeric_only = True,min_count = -1):       127个新项目,新块=自己._cython_agg_blocks(   -> 128方式,alt = alt,numeric_only = numeric_only,min_count = min_count       129)       130返回self._wrap_agged_blocks(new_items,new_blocks)

     

〜/ anaconda3 / envs / sn / lib / python3.7 / site-packages / pandas / core / groupby / generic.py   在_cython_agg_blocks中(自己,如何,alt,仅数字,最小计数)       186       187如果len(new_blocks)== 0:   -> 188提高DataError(“没有要聚合的数字类型”)       189       190#重置块中的位置以与我们的

相对应      

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

0 个答案:

没有答案
相关问题