Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataframe Apply method to return multiple elements (series)

import pandas as pd

Let's say I have a dataframe like so:

df = pd.DataFrame({"a":range(4),"b":range(1,5)})

it looks like this:

   a  b
0  0  1
1  1  2
2  2  3
3  3  4

and a function that multiplies X by Y:

def XtimesY(x,y):
    return x*y

If I want to add a new pandas series to df I can do:

df["c"] =df.apply( lambda x:XtimesY(x["a"],2), axis =1)

It works !

Now I want to add multiple series:

I have this function:

def divideAndMultiply(x,y):
    return x/y, x*y

something like this ?:

df["e"], df["f"] = df.apply( lambda x: divideAndMultiply(x["a"],2) , axis =1)

It doesn't work !

I want the 'e' column to receive the divisions and 'f' column the multiplications !

Note: This is not the code I'm using but I'm expecting the same behavior.

like image 768
YOBA Avatar asked Apr 13 '16 13:04

YOBA


People also ask

Can DataFrame apply return multiple columns?

Return Multiple Columns from pandas apply() You can return a Series from the apply() function that contains the new data. pass axis=1 to the apply() function which applies the function multiply to each row of the DataFrame, Returns a series of multiple columns from pandas apply() function.

How do I apply a function to multiple columns in pandas?

Pandas apply() Function to Single & Multiple Column(s) Using pandas. DataFrame. apply() method you can execute a function to a single column, all and list of multiple columns (two or more).

Can DataFrame can contain multiple series?

You can create a DataFrame from multiple Series objects by adding each series as a columns. By using concat() method you can merge multiple series together into DataFrame.

How do I return two columns in pandas DataFrame?

Return multiple columns using Pandas apply() method Objects passed to the pandas. apply() are Series objects whose index is either the DataFrame's index (axis=0) or the DataFrame's columns (axis=1). By default (result_type=None), the final return type is inferred from the return type of the applied function.


2 Answers

Almost there. Use zip* to unpack the function. Try this:

def divideAndMultiply(x,y):
    return x/y, x*y

df["e"], df["f"] = zip(*df.a.apply(lambda val: divideAndMultiply(val,2)))
like image 122
Sam Avatar answered Nov 15 '22 00:11

Sam


UPDATE

Updated for version 0.23 - using result_type='broadcast' for further details refer to documentation

Redefine your function like this:

def divideAndMultiply(x,y):
    return [x/y, x*y]

Then do this:

df[['e','f']] = df.apply(lambda x: divideAndMultiply(x["a"], 2), axis=1, result_type='broadcast')

You shall get the desired result:

In [118]: df
Out[118]:
   a  b  e  f
0  0  1  0  0
1  1  2  0  2
2  2  3  1  4
3  3  4  1  6
like image 36
Abbas Avatar answered Nov 14 '22 23:11

Abbas