Python 尝试使用pandas和numpy创建透视表

Python 尝试使用pandas和numpy创建透视表,python,pandas,numpy,Python,Pandas,Numpy,我使用以下方法尝试获取一个数据透视表,该数据透视表将数量乘以有效价格,并按产品名称对其进行分组: df\u排序的.pivot\u表(值=['Quantity','EffectivePrice'],索引=['ProductName'],aggfunc=np.multiply) 这是堆栈跟踪-不确定为什么这不起作用 ValueError Traceback (most recent call last) /usr/local/lib/py

我使用以下方法尝试获取一个数据透视表,该数据透视表将数量乘以有效价格,并按产品名称对其进行分组:

df\u排序的.pivot\u表(值=['Quantity','EffectivePrice'],索引=['ProductName'],aggfunc=np.multiply)

这是堆栈跟踪-不确定为什么这不起作用

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.9/site-packages/pandas/core/groupby/generic.py in aggregate(self, func, engine, engine_kwargs, *args, **kwargs)
    970                 try:
--> 971                     result = self._aggregate_multiple_funcs([func], _axis=self.axis)
    972 

/usr/local/lib/python3.9/site-packages/pandas/core/base.py in _aggregate_multiple_funcs(self, arg, _axis)
    544         if not len(results):
--> 545             raise ValueError("no results")
    546 

ValueError: no results

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-35-f616d7b46a13> in <module>
----> 1 df_sorted.pivot_table(values=['Quantity', 'EffectivePrice'], index=['ProductName'], aggfunc=np.multiply )

/usr/local/lib/python3.9/site-packages/pandas/core/frame.py in pivot_table(self, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name, observed)
   6824         from pandas.core.reshape.pivot import pivot_table
   6825 
-> 6826         return pivot_table(
   6827             self,
   6828             values=values,

/usr/local/lib/python3.9/site-packages/pandas/core/reshape/pivot.py in pivot_table(data, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name, observed)
    110 
    111     grouped = data.groupby(keys, observed=observed)
--> 112     agged = grouped.agg(aggfunc)
    113     if dropna and isinstance(agged, ABCDataFrame) and len(agged.columns):
    114         agged = agged.dropna(how="all")

/usr/local/lib/python3.9/site-packages/pandas/core/groupby/generic.py in aggregate(self, func, engine, engine_kwargs, *args, **kwargs)
    981                         # raised directly by _aggregate_multiple_funcs
    982                         raise
--> 983                     result = self._aggregate_frame(func)
    984                 except AttributeError:
    985                     # catch exception from line 969

/usr/local/lib/python3.9/site-packages/pandas/core/groupby/generic.py in _aggregate_frame(self, func, *args, **kwargs)
   1173         if axis != obj._info_axis_number:
   1174             for name, data in self:
-> 1175                 fres = func(data, *args, **kwargs)
   1176                 result[name] = fres
   1177         else:

ValueError: invalid number of arguments```
ValueError回溯(最近一次调用)
/聚合usr/local/lib/python3.9/site-packages/pandas/core/groupby/generic.py(self、func、engine、engine_-kwargs、*args、**kwargs)
970尝试:
-->971结果=self.\u聚合\u多个\u函数([func],\u轴=self.axis)
972
/usr/local/lib/python3.9/site-packages/pandas/core/base.py in\u aggregate\u multiple\u funcs(self、arg、axis)
544如果不是len(结果):
-->545提高值错误(“无结果”)
546
ValueError:没有结果
在处理上述异常期间,发生了另一个异常:
ValueError回溯(最近一次调用上次)
在里面
---->1个df_排序的.pivot_表(值=['Quantity','EffectivePrice'],索引=['ProductName'],aggfunc=np.multiply)
/pivot_表中的usr/local/lib/python3.9/site-packages/pandas/core/frame.py(self、value、index、columns、aggfunc、fill_value、margins、dropna、margins_name、observed)
6824从pandas.core.reforme.pivot导入pivot_表
6825
->6826回转台(
6827自我,
6828值=值,
/pivot_表中的usr/local/lib/python3.9/site-packages/pandas/core/reforme/pivot.py(数据、值、索引、列、aggfunc、填充值、边距、dropna、边距_名称、观察值)
110
111 grouped=data.groupby(键,观察到的=观察到的)
-->112 agged=grouped.agg(aggfunc)
113如果dropna和isinstance(聚集,ABCDATA帧)和len(聚集列):
114 agged=agged.dropna(how=“all”)
/聚合usr/local/lib/python3.9/site-packages/pandas/core/groupby/generic.py(self、func、engine、engine_-kwargs、*args、**kwargs)
981#由_聚合_多重函数直接引发
982提高
-->983结果=自聚集帧(func)
984除属性错误外:
985#从第969行捕获异常
/usr/local/lib/python3.9/site-packages/pandas/core/groupby/generic.py在聚合框架中(self、func、*args、**kwargs)
1173如果轴!=对象信息轴编号:
1174用于名称,数据在self中:
->1175 fres=func(数据,*args,**kwargs)
1176结果[名称]=fres
1177其他:
ValueError:参数数无效```

我的理解是,您不能在透视表中应用多列操作。可能使用
groupby+transform
可以。根据解释,我建议使用此代码(我不确定预期结果是否符合您的要求):

对于此示例数据帧:

   Quantity  EffectivePrice ProductName
0         1              12           A
1         1              13           B
2         2              14           A
输出如下:

ProductName
A    40
B    13

你能发布一个样本数据集吗?
ProductName
A    40
B    13