2

I have two data frames, one (p) df contains columns to be transformed, second (a) contains transformation parameter in form of pd.series:

p=np.random.rand(5,3) #create data frame
cols=["A","B","C"]
df1=pd.DataFrame(p,columns=cols)
a=np.array([0.3,0.4,0.5]) # create series of transform parameters
a=pd.Series(a,index=cols)

I wander how to iterate over df columns to transform each one with appropriate transform parameter, something like below:

df1.apply(stats.boxcox,lmbda=a)

which of course not works. My temporary solution is just a brute force function:

def boxcox_transform(df,lambdas):
    df1=pd.DataFrame(index=df.index)
    for column in list(df):
        df1[column]=stats.boxcox(df[column],lambdas[column])
    return(df1)
boxcox_transform(df1,a)

I wander is there any more elegant solution like for example R CRAN mapply which can iterate over two lists

1 Answer 1

2

You can use a lambda:

result_df = df1.apply(lambda col: stats.boxcox(col, a.loc[col.name]))
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect. Thanks!
May I ask what is a?
@ju. Look at the question. It's a Series defined by the asker.

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.