Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most pythonic way to apply a function on and return multiple columns?

Tags:

python

pandas

While using Pandas, I often encounter a case where there is an existing function which takes in multiple arguments and returns multiple values:

def foo(val_a, val_b):
    """
    Some example function that takes in and returns multiple values.
    Can be a lot more complex.
    """
    sm = val_a + val_b
    sb = val_a - val_b
    mt = val_a * val_b
    dv = val_a / val_b
    return sm, sb, mt, dv

Suppose I have a dataframe:

import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8]])
df
Out[6]: 
   0  1
0  1  2
1  3  4
2  5  6
3  7  8

What I want is to apply foo on df with column 0 and 1 as arguments, and put the results into new columns of df, without modifying foo, like this:

df_out
Out[7]:
   0  1  su  sb  mt  dv
0  1  2  3   -1  2   0.5
1  3  4  7   -1  12  0.75
2  5  6  11  -1  30  0.833
3  7  8  15  -1  56  0.875

What is the most pythonic way to achieve this?

like image 233
lpounng Avatar asked Oct 17 '22 12:10

lpounng


2 Answers

You can use apply + DataFrame constructor:

cols = ['sm','sb','mt','dv']
df[cols] = pd.DataFrame(df.apply(lambda x: foo(x[0], x[1]), 1).values.tolist(),columns= cols)
print (df)
   0  1  sm  sb  mt        dv
0  1  2   3  -1   2  0.500000
1  3  4   7  -1  12  0.750000
2  5  6  11  -1  30  0.833333
3  7  8  15  -1  56  0.875000

Solution with concat

cols = ['sm','sb','mt','dv']
df[cols] = pd.concat(foo(df[0], df[1]), axis=1, keys=cols)
print (df)
   0  1  sm  sb  mt        dv
0  1  2   3  -1   2  0.500000
1  3  4   7  -1  12  0.750000
2  5  6  11  -1  30  0.833333
3  7  8  15  -1  56  0.875000

Also is possible create new DataFrame and then concat original:

cols = ['sm','sb','mt','dv']
df1 = pd.concat(foo(df[0], df[1]), axis=1, keys=cols)
print (df1)
   sm  sb  mt        dv
0   3  -1   2  0.500000
1   7  -1  12  0.750000
2  11  -1  30  0.833333
3  15  -1  56  0.875000

df = pd.concat([df, df1], axis=1)
print (df)
   0  1  sm  sb  mt        dv
0  1  2   3  -1   2  0.500000
1  3  4   7  -1  12  0.750000
2  5  6  11  -1  30  0.833333
3  7  8  15  -1  56  0.875000
like image 55
jezrael Avatar answered Oct 20 '22 10:10

jezrael


#apply function foo and generate a DF using return values and then merge into existing DF.
merged = pd.merge(df,df.apply(lambda x: pd.Series(foo(x[0],x[1])),axis=1),left_index=True,right_index=True)
#change column names.
merged.columns=[0,1,'sm','sb','mt','dv']

merged
Out[1478]: 
   0  1    sm   sb    mt        dv
0  1  2   3.0 -1.0   2.0  0.500000
1  3  4   7.0 -1.0  12.0  0.750000
2  5  6  11.0 -1.0  30.0  0.833333
3  7  8  15.0 -1.0  56.0  0.875000
like image 40
Allen Avatar answered Oct 20 '22 09:10

Allen