I am trying to use a lambda function to create several new columns at once in pandas. I know this works if the function returns a single value but if multiple values are returned it cannot unpack properly. I have created a small reproducible example to illustrate.
I have a small DataFrame of 6 numbers in the column number
:
lst = [1,2,3,4,5,6]
df = pd.DataFrame(lst, columns=['number'])
df
number
0 1
1 2
2 3
3 4
4 5
5 6
If I use this custom function that returns only a single value within a lambda, I am able to create a new column with ease:
def add_one(number):
new_number = number + 1
return new_number
df['add_one'] = df.number.apply(lambda x: add_one(x))
number add_one
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
However if I use a slightly more complex function that returns more than one value it does not know how to unpack this correctly:
def multiply_bylist(number, multiple, string):
num = number * multiple
word = string + str(number)
return num, word
df['multiple'], df['words'] = df.number.apply(lambda x: multiply_bylist(x, 5, 'This is number:'))
ValueError: too many values to unpack (expected 2)
I was hoping for the final DataFrame to look as such:
number add_one multiple words
0 1 2 5 'This is number:1'
1 2 3 10 'This is number:2'
2 3 4 15 'This is number:3'
3 4 5 20 'This is number:4'
4 5 6 25 'This is number:5'
5 6 7 30 'This is number:6'
Is this possible or not? Thanks