I have a dataframe
>>>df = pd.DataFrame({"a" : [1,2,3], "b" :[4,5,6], "c":[10,11,12]})
a b c
0 1 4 10
1 2 5 11
2 3 6 12
and a function that returns multiple values.
>>>def my_fun(values):
>>> return(values+10, values*3)
It works on a single column:
>>>res_1, res_2 = my_fun(df['a'])
>>>print(res_1)
0 3
1 6
2 9
>>>print(res_2)
0 11
1 12
2 13
But when I try to use apply to get two dataframes as result, I get an error.
>>>res_1, res_2 = df.apply(my_fun, axis=0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-173-142501cd22f6> in <module>()
23 # return(values_2+10, values_2*3)
24
---> 25 res_1, res_2 = df.apply(my_fun, axis=0)
ValueError: too many values to unpack (expected 2)
Any clue? Note that this is just an ilustrative example.
UPDATE:
I am really aiming at applying a function columnwise rather than other workaround (as this example is only ilustrative). The example provided above is ambiguous, a better one would be the following, where I want to add average of each column:
>>>import numpy as np
>>>def my_fun_2(values):
>>> return(values+np.mean(values), values*3)
df.apply(my_fun, axis=0)is not returning two values. Return the value of that function to a single variable, then check/assign that result accordingly.axis=1. It would help if you showed what your desired output was.