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.
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.
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).
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.
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.
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)))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With