How do I create a new dataframe from another existing one by applying a changing formula?

Question:

I have the following dataframe :

df_1 = pd.DataFrame({
    'col1' : [20, 60, 55, 80, 32, 33],
    'col2' : [10, 16, 18, 12, 19, 20],
    'col3' : [5, 2, 7, 9, 1, 2]
})

It has the following display:

   col1 col2 col3
0   20  10  5
1   60  16  2
2   55  18  7
3   80  12  9
4   32  19  1
5   33  20  2

From this df_1 I want to get df_2 by applying to its columns the following formula : (x**coef - 1) / coef where coef = [0.2, 0.3, 0.4] so df_2 will be:

    col1        col2        col3
0   4.102821    3.317541    2.259135
1   6.339666    4.324656    0.798770
2   6.144037    4.600088    2.944766
3   7.011244    3.691453    3.520562
4   5.000000    4.729818    0.000000
5   5.061733    4.854854    0.798770

To get df_2 I used the follwoing code:

coefs = [0.2, 0.3, 0.4]
df_2 = pd.DataFrame()
cols = df_1.columns
df_2[cols[0]] = (df_1[cols[0]]**coefs[0] - 1)/ coefs[0] 
df_2[cols[1]] = (df_1[cols[1]]**coefs[1] - 1)/ coefs[1]
df_2[cols[2]] = (df_1[cols[2]]**coefs[2] - 1)/ coefs[2]

There is a way to apply the formula by using some indexing / loop or any other tips ?

Any help from your side will be highly appreciated (upvoted indeed !)

Best regards !

Asked By: Khaled DELLAL

||

Answers:

You can copy original dataframe with method .copy() (so that changes to the new dataframe do not change the original) and use for the new dataframe method .apply() with lambda x. Also, there is no need to create a list with column names, you can use the .iloc[] method with the column index:

coefs = [0.2, 0.3, 0.4]
df_2 = df_1.copy()
for i in range(len(coefs)):
    df_2.iloc[:,i] = df_2.iloc[:,i].apply(lambda x: (x**coefs[i]-1)/coefs[i])
Answered By: stukituk
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.