pandas apply function that returns multiple values to rows in pandas dataframe pandas apply function that returns multiple values to rows in pandas dataframe pandas pandas

pandas apply function that returns multiple values to rows in pandas dataframe


Return Series and it will put them in a DataFrame.

def myfunc(a, b, c):    do something    return pd.Series([e, f, g])

This has the bonus that you can give labels to each of the resulting columns. If you return a DataFrame it just inserts multiple rows for the group.


Based on the excellent answer by @U2EF1, I've created a handy function that applies a specified function that returns tuples to a dataframe field, and expands the result back to the dataframe.

def apply_and_concat(dataframe, field, func, column_names):    return pd.concat((        dataframe,        dataframe[field].apply(            lambda cell: pd.Series(func(cell), index=column_names))), axis=1)

Usage:

df = pd.DataFrame([1, 2, 3], index=['a', 'b', 'c'], columns=['A'])print df   Aa  1b  2c  3def func(x):    return x*x, x*x*xprint apply_and_concat(df, 'A', func, ['x^2', 'x^3'])   A  x^2  x^3a  1    1    1b  2    4    8c  3    9   27

Hope it helps someone.


Just return a list instead of tuple.

In [81]: dfOut[81]:                             x         y         zts                                               2014-05-15 10:38:00  0.120117  0.987305  0.1162112014-05-15 10:39:00  0.117188  0.984375  0.1220702014-05-15 10:40:00  0.119141  0.987305  0.1191412014-05-15 10:41:00  0.116211  0.984375  0.1201172014-05-15 10:42:00  0.119141  0.983398  0.118164[5 rows x 3 columns]In [82]: def myfunc(args):   ....:        e=args[0] + 2*args[1]   ....:        f=args[1]*args[2] +1   ....:        g=args[2] + args[0] * args[1]   ....:        return [e,f,g]   ....: In [83]: df.apply(myfunc ,axis=1)Out[83]:                             x         y         zts                                               2014-05-15 10:38:00  2.094727  1.114736  0.2348032014-05-15 10:39:00  2.085938  1.120163  0.2374272014-05-15 10:40:00  2.093751  1.117629  0.2367702014-05-15 10:41:00  2.084961  1.118240  0.2345122014-05-15 10:42:00  2.085937  1.116202  0.235327