Skip to content
Advertisement

Using result_type with pandas apply function

I want to use apply on a pandas.DataFrame that I created, and return for each row a list of values, where each value is a column in itself.

I wrote the following code:

JavaScript

When I add result_type='expand' in order to change the returned array into separate columns I get the following error:

JavaScript

However if I drop the result_type field it runs fine (returns a column of arrays), what might be the problem?

  • I’m using colab to run my code

Advertisement

Answer

This code works in pandas version 0.23.3, properly you just need to run pip install --upgrade pandas in your terminal.

Or

You can accomplish it without the result_type as follows:

JavaScript

BTW, you don’t need a lambda for it, you can just:

JavaScript

Update The result_type was announced in the release notes of pandas 0.23: https://pandas.pydata.org/pandas-docs/stable/whatsnew.html#whatsnew-0230 so I am afraid you will have to update.

Advertisement