Home > Software design >  Using Apply Method with function that requires list inputs from multiple columns
Using Apply Method with function that requires list inputs from multiple columns

Time:04-29

I have been trying to use the lambda and apply() method to create a new column based on other columns. The calculation I want to do calculates a range of "Stage Reaction" values based on a range of "Alpha 1" values and "Alpha 2" values, along with being based on a constant "Stage Loading" value. Code below:

import pandas as pd
import numpy as np

data = {
        'Stage Loading':[0.1], 
        'Alpha 1':[[0.1,0.12,0.14]], 
        'Alpha 2':[[0.1,0.12,0.14]]
       }

pdf = pd.DataFrame(data)


def findstageloading(row):
    stload = row('Stage Loading')
    for alpha1, alpha2 in zip(row('Alpha 1'), row('Alpha 2')):
        streact = 1 - 0.5 * stload * (np.tan(alpha1) - np.tan(alpha2))
        return streact


pdf['Stage Reaction'] = pdf.apply(lambda row: findstageloading, axis = 1)

print(pdf.to_string())

The problem is that this code returns a message

"<function findstageloading at 0x000002272AF5F0D0>" 

for the new column.

Can anyone tell me why? I want it to return a list of values

[0.9420088227267556, 1.0061754635552815, 1.0579911772732444]

CodePudding user response:

Your lambda is just returning the function, just use pdf['Stage Reaction'] = pdf.apply(findstageloading, axis = 1)

Also, you need square brackets to access columns not round ones, otherwise python thinks you're calling a function.

Also, I'm not sure where your output came from but if you want to do pairwise arithmetic, you can use vectorisation and omit the zip.

def findstageloading(row):
    stload = row['Stage Loading'] # not row('Stage Loading')
    alpha1, alpha2 = row['Alpha 1'], row['Alpha 2']
    streact = 1 - 0.5 * stload * (np.tan(alpha1) - np.tan(alpha2))
    return streact
  • Related