Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据透视表中的多维乘法_Python_Arrays_Pandas_Numpy_Multidimensional Array - Fatal编程技术网

Python 数据透视表中的多维乘法

Python 数据透视表中的多维乘法,python,arrays,pandas,numpy,multidimensional-array,Python,Arrays,Pandas,Numpy,Multidimensional Array,考虑这样一个数据透视表: E A B C D bar one large 4 6 small 5 8 two large 7 9 small 6 9 foo one large 2 9 small 1 2 two small 3 11 我想将具有A=bar的每个E条目乘以l和A=foo乘以m。对于具有B=1的条目,我想将它们乘以n,对于B=2乘以

考虑这样一个数据透视表:

                  E
A   B   C     D    
bar one large 4   6
        small 5   8
    two large 7   9
        small 6   9
foo one large 2   9
        small 1   2
    two small 3  11
我想将具有
A=bar
的每个
E
条目乘以
l
A=foo
乘以
m
。对于具有
B=1
的条目,我想将它们乘以
n
,对于
B=2
乘以
p
。对于每个维度的每个级别,我都有一个不同的值,我想乘以
E
。结果表将使
E
中的每个原始值乘以[表中的维度数(四)]变量

在Python中实现这一点的最快方法是什么?我的实际表格是高维的,作为优化过程的一部分,需要多次执行此操作

我使用以下代码创建了透视表:

df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                         "bar", "bar", "bar", "bar"],
                   "B": ["one", "one", "one", "two", "two",
                         "one", "one", "two", "two"],
                   "C": ["small", "large", "large", "small",
                         "small", "large", "small", "small",
                         "large"],
                   "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
                   "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})

table = pd.pivot_table(df, values='D', index=['A', 'B', 'C', 'D'], aggfunc=np.sum)
要乘以的值存储在字典中

例如:

{'A': {'bar': 0.5, 'foo': 0.2}, 
'B': {'one': 0.1, 'two': 0.3},
'C': {'large': 2, 'small': 4},
'D': {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70}} 

使用此词典,第一行的结果将是6*0.5*0.1*2*40=24。

您可以在索引的每个级别使用
map

其中
d
是你在问题中给出的词典

table['Emult'] = table['E']*np.prod([table.index.get_level_values(lv).map(d[lv]) 
                                     for lv in table.index.names], 
                                    axis=0)
print (table)
                  E  Emult
A   B   C     D           
bar one large 4   6   24.0
        small 5   8   80.0
    two large 7   9  189.0
        small 6   9  324.0
foo one large 2   9    7.2
        small 1   2    1.6
    two small 3  11   79.2