Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

Say for instance, I am having a dataframe with columns named 'ID', 'col_1', 'col_2'. And I define a method named f as below:

f = lambda x, y : my_function_expression.

Now I want to apply the function f to the dataframe's two columns 'col_1', 'col_2' to element-wise calculate a new column 'col_3', somewhat like :

df['col_3'] = df[['col_1','col_2']].apply(f)  

# Pandas gives : TypeError: ('<lambda>() takes exactly 2 arguments (1 given)'

How to do it?

** Add detail sample as below ***

import pandas as pd

df = pd.DataFrame({'ID':['1','2','3'], 'col_1': [0,2,3], 'col_2':[1,4,5]})

mylist = ['a','b','c','d','e','f']

def get_sublist(sta,end):

    return mylist[sta:end+1]

#df['col_3'] = df[['col_1','col_2']].apply(get_sublist,axis=1)

# expect above to output df as below 

  ID  col_1  col_2            col_3

0  1      0      1       ['a', 'b']

1  2      2      4  ['c', 'd', 'e']

2  3      3      5  ['d', 'e', 'f']

1 Answer

0 votes
by (107k points)

Here's an instance of working with the apply() function on the dataframe, which I am calling with axis = 1.

Note that the difference is that instead of passing two values to the function f, rewrite the function to input a pandas Series object, and then index the Series to get the values needed.

In [49]: df

Out[49]: 

          0         1

0  1.000000  0.000000

1 -0.494375  0.570994

2  1.000000  0.000000

3  1.876360 -0.229738

4  1.000000  0.000000

In [50]: def f(x):    

   ....:  return x[0] + x[1]  

   ....:  

In [51]: df.apply(f, axis=1) #passes a Series object, row-wise

Out[51]: 

0    1.000000

1    0.076619

2    1.000000

3    1.646622

4    1.000000

Depending on your use case, it is sometimes helpful to create a pandas group object, and then use apply it to the group. 

Want to become a Python Developer? Check out this insightful Python Certification course.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...