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?
bianco2? Try posting a minimal reproducible example.