Python apply class method to row of data frame
Question: 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.


Answer: Just use a lambda function?

`xxx = df.apply(lambda x: A(x),axis=1)`

edit: Another solution is to directly pass the class, the apply-function then
calls the constructor:

`xxx = df.apply(A,axis=1)`

this works:

    
    
    import pandas as pd 
    
    class C(object):
        def __init__(self,dat):
            return
    
    A = pd.DataFrame({'a':pd.Series([1,2,3])})
    A.apply(lambda x: C(x),axis=1)
    

