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
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:
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.