Skip to content
Advertisement

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

I have a dataframe with a timeindex and 3 columns containing the coordinates of a 3D vector:

JavaScript

I would like to apply a transformation to each row that also returns a vector

JavaScript

but if I do:

JavaScript

I end up with a Pandas series whose elements are tuples. This is beacause apply will take the result of myfunc without unpacking it. How can I change myfunc so that I obtain a new df with 3 columns?

Edit:

All solutions below work. The Series solution does allow for column names, the List solution seem to execute faster.

JavaScript

Advertisement

Answer

Just return a list instead of tuple.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement