I have a list of strings stored in a pandas dataframe df, with column name of text (i.e. df['text']).
I have a function f(text: str) -> (int, int, int).
Now, I want to do the following.
df['a'], df['b'], df['c'] = df['text'].apply(f)
How can I create three columns with the three returned values from the function?
The above code gives the error of
ValueError: too many values to unpack (expected 3)
I tried
df['a', 'b', 'c'] = df['text'].apply(f)
but I get one column with the name of 'a', 'b', 'c'
NB:
- There is a similar question in SO, but when I use the following solution from there, I again get an error.
df[['a', 'b', 'c']] = df['text'].apply(f, axis=1, result_type='expand')
The error is
f() got an unexpected keyword argument 'axis'
f() got an unexpected keyword argument 'result_type' #(once I remove the axis=1 parameter)
- Note that
dfhas other columns as well