I have a dataframe where each row represents a parameter set for a parameterized class. For each row, I want to compute a value based on the parameters and create a new column representing that computation. Is there a canonical way to achieve this? The following code works, but it’s instantiating a new object for every row. Would it be more computationally efficient to instantiate a single object and reuse it by updating it’s parameters instead of creating a new object for every row?
To be clear, the following code achieves my goal, but I’m wondering if there is a better way to do it. And i’m generally wondering about updating a parameter set on an instantiated object from a dictionairy.
import pandas as pd import numpy as np import param as pm df = pd.DataFrame(np.random.randn(30, 2), columns=['a','b']) class Test(pm.Parameterized): a = pm.Number() b = pm.Number() def c(self): return self.a + self.b df['c'] = [Test(**ab).c() for ab in df.to_dict(orient='records')]
I’m imagining something like this:
t = Test() df['c'] = [t.params.set(**ab).c() for ab in df.to_dict(orient='records')]