I'm trying to take a dictionary and (1) raise each key to the power of its value, and (2) multiply together the results of (1).
This needs to be done to several million rows of data. Any thoughts on how to speed up the process? I am using pandas data frame's map function to apply the function toTheExp
to my column of data already. This step is still pretty slow though. Currently I'm trying something like:
import ast
from numpy import array
from functools import reduce
import operator
def toTheExp(d):
return reduce(operator.mul, array(d.keys())**array(d.values()), 1)
toTheExp({2:3,4:5,6:7})
>>> 2293235712
Since you're importing numpy, I assume it's fine to use it over pandas.
so that:
A quick speed test with 10 million entries:
and your code gives:
Your code isn't so slow, really. Perhaps the bottleneck is somewhere else? Also, the product of tens of millions of numbers is generally either zero, one, or infinity... Just want to make sure that you're doing something sensical here.