2

So I have a large amount of columns within a pandas dataframe and I need to pass groups of them through a function. The function is large but I'll create an example below. I am not sure how to pass the reference of df.varName to the function without getting the issue of the variable not being defined. When I try a function such as:

def bianco2(df, varX, varT):
    stdX = np.std(df.varX)
    stdT = np.std(df.varT)
    newVar = stdX + stdT
    return newVar

I get the error that varX isn't defined. So I wrote the function where I would pass the whole phrase:

def bianco3(varX, varT):
    stdX = np.std(varX)
    stdT = np.(varT)
    newVar = stdX + stdT
    return newVar

Where "varX = df.varX".

This worked but isn't practical for a large number of variables because I would still have to manually update each varX and varT. So I tried creating a list of variables in the format df.varX and then using a for loop to pass the list of variables. The issue is python sees it as a string and not a reference. I looked at using functools.partial but was unsuccessful.

Any ideas on how to write this in a simple format and be able to pass panda columns to a function?

1

1 Answer 1

5

You may want to try this ?

def bianco2(df, varX, varT):
    stdX = np.std(df[varX])
    stdT = np.std(df[varT])
    newVar = stdX + stdT
    return newVar

print bianco2(df,'Customer','Policy')

input

   Policy  Customer  Employee CoveredDate   LapseDate
0     123      1234      1234  2011-06-01  2013-01-01
1     124      1234      1234  2016-01-01  2013-01-01
2     124      5678      5555  2014-01-01  2013-01-01

output

  2095.39309492
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Shijo! That should simplify my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.