Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

import pandas as pd

def get_list(row):
  return [i for i in range(5)]

df = pd.DataFrame(0, index=np.arange(100), columns=['col'])


df.apply(lambda row: get_list(row), axis=1, result_type='expand')

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

TypeError: ("<lambda>() got an unexpected keyword argument 'result_type'", 'occurred at index 0')

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
like image 811
Ak-Mo Avatar asked Oct 17 '18 11:10

Ak-Mo


People also ask

How do I apply a function in pandas?

Pandas DataFrame apply() MethodThe apply() method allows you to apply a function along one of the axis of the DataFrame, default 0, which is the index (row) axis.

How will you apply a function to a column of pandas DataFrame?

DataFrame - apply() function. The apply() function is used to apply a function along an axis of the DataFrame. Objects passed to the function are Series objects whose index is either the DataFrame's index (axis=0) or the DataFrame's columns (axis=1).

How will you apply a function to a row of pandas DataFrame?

Use apply() function when you wanted to update every row in pandas DataFrame by calling a custom function. In order to apply a function to every row, you should use axis=1 param to apply(). By applying a function to each row, we can create a new column by using the values from the row, updating the row e.t.c.

What is the use of apply () in Python explain in detail?

apply() method. This function acts as a map() function in Python. It takes a function as an input and applies this function to an entire DataFrame. If you are working with tabular data, you must specify an axis you want your function to act on ( 0 for columns; and 1 for rows).


1 Answers

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:

def get_list(row):
    return pd.Series([i for i in range(5)])

df = pd.DataFrame(0, index=np.arange(100), columns=['col'])
pd.concat([df, df.apply(get_list, axis=1)], axis=1)

    col 0   1   2   3   4
0   0   0   1   2   3   4
1   0   0   1   2   3   4
2   0   0   1   2   3   4
3   0   0   1   2   3   4
4   0   0   1   2   3   4
...

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

df.apply(get_list, axis=1, result_type='expand')

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.

like image 51
iDrwish Avatar answered Sep 20 '22 08:09

iDrwish