1

I have a function foo() which takes two arguments as two columns of pandas named year and month and return a list of four numbers.

  df['A'],df['B'],df['C'],df['D']=  df.apply(lambda x: foo(x.year, x.month), axis=1,result_type="expand")

It just gives me four columns named A,B,C,D with 0,1,2,3 respectively filled in them. What am I doing wrong?

Kindly don't answer for single variable outputs or single variable arguments. There are plenty of examples out there for that. Thankyou so much for the help.

1
  • Can you add more part of function and data examples so that we can recreate the environment you are working in? Commented Jun 6, 2020 at 12:10

1 Answer 1

1

You are incorrectly assigning the results of df.apply with optional parameter result_type='expand', instead, Use:

df[['A', 'B', 'C', 'D']] =  df.apply(lambda x: foo(x.year, x.month), axis=1, result_type="expand")

Consider the example demonstrating this,

df = pd.DataFrame({'col1': np.arange(5), 'col2': np.arange(5) * 2})

#print(df)
   col1  col2
0     0     0
1     1     2
2     2     4
3     3     6
4     4     8

Reproducing the problem situation,

df['A'], df['B'] = df.apply(lambda s: (s['col1']**2, s['col2']**2), axis=1, result_type='expand')

#print(df)
   col1  col2  A  B
0     0     0  0  1
1     1     2  0  1
2     2     4  0  1
3     3     6  0  1
4     4     8  0  1

The solution is,

df[['A', 'B']] = df.apply(lambda s: (s['col1']**2, s['col2']**2), axis=1, result_type='expand')

#print(df)
   col1  col2   A   B
0     0     0   0   0
1     1     2   1   4
2     2     4   4  16
3     3     6   9  36
4     4     8  16  64

OR:

df['A'], df['B'] = df.apply(
lambda s: (s['col1']**2, s['col2']**2), axis=1, result_type='expand').T.to_numpy()

#print(df)
   col1  col2   A   B
0     0     0   0   0
1     1     2   1   4
2     2     4   4  16
3     3     6   9  36
4     4     8  16  64
Sign up to request clarification or add additional context in comments.

6 Comments

Can you explain a little what the difference is? And why was mine giving 0,1,2,3?
Well, I guess if you try to unpack the results of .apply with result_type='expand it just returns the headers of newly created columns (I guess its internal implementation behaviour).
@jazrael @anky Can you explain about the behaviour of result_type=expand used here? Why its returning headers when we try to unpack it?
When you unpack a dataframe , it by default chooses to return column headers example: print(list(df)) would return column names, for accessing the values you need to access the underlying array you need to use .to_numpy() : df['col3'],df['col4'] = df.apply(lambda s: (s['col1']**2, s['col2']**2), axis=1, result_type='expand').T.to_numpy()
@anky Makes sense.:), btw, Thanks for taking time out for explaining.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.