My class takes a row of a dataframe to construct an object and I would like to create an array of objects by applying init to every row of a dataframe. Is there a way to vectorize this? My class definition looks like
class A(object):
def __init__(self,row):
self.a = row['a']
self.b = row['b']
Any suggestion will be highly appreciated!
I have one way which I am not that satisfied with to solve this problem. Define another function outside of class and then use apply.
def InitA(row):
return A(row)
Assume df is the data frame I want to use as argument.
xxx = df.apply(InitA,axis=1)
gives what I want. However, I don't think InitA is necessary.
My original problem is a bit more complicated. The class definition is
class A(object):
def __init__(self):
return
def add_parameter(self,row):
self.a = row['a']
I intend to apply add_parameter to every row of a data frame. But I think defining another (lambda) function is necessary to solve this problem.