user2866103
user2866103

Reputation: 10555

using function wheen looping through dataframe python/pandas

I have a function that uses two colomns in a dataframe:

def create_time(var, var1):  
    if var == "Helår":  
        y = var1+'Q4'  
    else:  
        if var == 'Halvår':  
            y = var1+'Q2'  
        else:  
            y = var1+'Q'+str(var)[0:1]  
    return y   

Now i want to loop hrough my dataframe, creatring a new column using the function, where var and var1 are columns in the dataframe

I try with the following, but have no luck:

for row in bd.iterrows():
      A = str(bd['Var'])    
      B = str(bd['Var1'])
      bd['period']=create_time(A,B)

Upvotes: 1

Views: 117

Answers (1)

Dan Allan
Dan Allan

Reputation: 35235

Looping is a last resort. There is usually a "vectorized" way to operate on the entire DataFrame, which always faster and usually more readable too.

To apply your custom function to each row, use apply with the keyword argument axis=1.

bd['period'] = bd[['Var', 'Var1']].apply(lambda x: create_time(*x), axis=1)

You might wonder why it's not just bd.apply(create_time). Since create_time wants two arguments, we have to "unpack" the row x into its two values and pass those to the function.

Upvotes: 1

Related Questions