You can go with @greenAfrican example, if it's possible for you to rewrite your function. But if you don't want to rewrite your function, you can wrap it into anonymous function inside apply, like this:

>>> def fxy(x, y):
...     return x * y

>>> df['newcolumn'] = df.apply(lambda x: fxy(x['A'], x['B']), axis=1)
>>> df
    A   B  newcolumn
0  10  20        200
1  20  30        600
2  30  10        300
Answer from roman on Stack Overflow
๐ŸŒ
Medium
ogungbireadedolapo.medium.com โ€บ passing-multiple-arguments-into-pandas-apply-function-3d9cf89d95cc
Passing multiple arguments into Pandas Apply function | by Ogungbire Adedolapo | Medium
September 22, 2022 - To break down the above code, letโ€™s start from the first line. ... The function to be applied to the dataframe is first defined. Since multiple columns are employed here, the function takes more than one argument. Here in this demo, the function returns the multiplication of the values in the two columns. ... In the above snippet, the demo data is created and the data has three rows and two columns (labelled โ€˜Aโ€™ and โ€˜Bโ€™).
Discussions

python - Apply function with two arguments to columns - Stack Overflow
Can you make a pandas function with values in two different columns as arguments? I have a function that returns a 1 if two columns have values in the same range. otherwise it returns 0: def segmen... More on stackoverflow.com
๐ŸŒ stackoverflow.com
[Pandas] Why no argument required in apply() function?
Apply means "take this function and apply it to each row in the dataframe". The rows are passed, one by one, when Pandas calls the function internally. More on reddit.com
๐ŸŒ r/learnpython
15
1
February 27, 2022
python - Passing a function with multiple arguments to DataFrame.apply - Stack Overflow
0 Applying a function to several pandas columns and extra integers arguments More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to apply a function to two columns of Pandas dataframe - Stack Overflow
I found a related Q&A at below ... not 2 from 1 . stackoverflow.com/questions/12356501/โ€ฆ ... This allows f to be a user-defined function with multiple input values, and uses (safe) column names rather than (unsafe) numeric indices to access the columns. ... import pandas as pd df = ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.apply.html
pandas.DataFrame.apply โ€” pandas 3.0.1 documentation
However if the apply function returns a Series these are expanded to columns. ... Positional arguments to pass to func in addition to the array/series. ... Only has an effect when func is a listlike or dictlike of funcs and the func isnโ€™t a string. If โ€œcompatโ€, will if possible first translate the func into pandas methods (e.g.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-apply-a-function-to-multiple-columns-in-pandas
How to Apply a function to multiple columns in Pandas? - GeeksforGeeks
July 15, 2025 - Pandas.apply allow the users to pass a function and apply it on every single value of the Pandas series. ... In this example, we are passing only a single column and increment age with 2.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas apply() function to single & multiple column(s)
Pandas apply() Function to Single & Multiple Column(s) - Spark By {Examples}
December 6, 2024 - Using Pandas.DataFrame.apply() method you can execute a function to a single column, all, and a list of multiple columns (two or more). In this article, I
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ pandas-apply-function-with-two-arguments-to-columns.aspx
Python - Pandas apply function with two arguments to columns
December 21, 2023 - The apply() method passes the columns of each group in the form of a DataFrame inside the function which is described in apply() method. The function which is described inside the apply() method returns a series or DataFrame (NumPy array or even a list). ... # Importing pandas package import ...
๐ŸŒ
Statology
statology.org โ€บ home โ€บ pandas: how to apply function to multiple columns
Pandas: How to Apply Function to Multiple Columns
April 19, 2024 - The easiest way to do this is by using the lambda function inside of the apply() function in pandas. ... This particular example applies the function named f to the points and assists column of the DataFrame and stores the results in a new column ...
Find elsewhere
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ pandas-dataframe-apply-examples
Pandas DataFrame apply() Examples | DigitalOcean
August 3, 2022 - In the first example, the sum of elements along the column is calculated. Whereas in the second example, the sum of the elements along the row is calculated. Letโ€™s say we want to apply a function that accepts more than one parameter. In that case, we can pass the additional parameters using the โ€˜argsโ€™ argument. import pandas as pd def sum(x, y, z): return x + y + z df = pd.DataFrame({'A': [1, 2], 'B': [10, 20]}) df1 = df.apply(sum, args=(1, 2)) print(df1)
๐ŸŒ
Pandas
pandas.pydata.org โ€บ pandas-docs โ€บ version โ€บ 0.20.2 โ€บ generated โ€บ pandas.DataFrame.apply.html
pandas.DataFrame.apply โ€” pandas 0.20.2 documentation
Objects passed to functions are Series objects having index either the DataFrameโ€™s index (axis=0) or the columns (axis=1). Return type depends on whether passed function aggregates, or the reduce argument if the DataFrame is empty.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ [pandas] why no argument required in apply() function?
r/learnpython on Reddit: [Pandas] Why no argument required in apply() function?
February 27, 2022 -
import pandas as pd

myDF = pd.DataFrame({'student_names':['Monserta ruff','Gonzalo Fryer','Kris Venmeter'],'grades':[34,58,100]})

def assign_letter(row):
    if row >= 90:
        result = 'A**'
    elif row >=50:
        result = 'C'
    else:
        result ='F'
    return result
myDF['letter-grades'] = myDF['grades'].apply(assign_letter) #no argument required?
myDF

Why doesn't the function assign_letter(row) require an argument (no parenthesis) but it still gives the CORRECT Resultant DF (as below)?

student_namesgradesletter_grades
Monserta ruff34F
Gonzalo Fryer58C
Kris Venmeter100A**

๐ŸŒ
W3docs
w3docs.com โ€บ python
How to apply a function to two columns of Pandas dataframe
To do this, you can define the function with multiple arguments and pass the columns to the function as separate arguments. ... import pandas as pd # Load the data df = pd.read_csv('data.csv') # Define the function def multiply(value_1, value_2): ...
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ how-to-apply-a-function-with-multiple-arguments-to-create-a-new-pandas-column.aspx
How to apply a function with multiple arguments to create a new Pandas column?
September 22, 2023 - Given a Pandas DataFrame, we have to apply a function with multiple arguments. We can insert a new column in a DataFrame whose values are defined from a function which takes multiple arguments.
Top answer
1 of 16
717

There is a clean, one-line way of doing this in Pandas:

df['col_3'] = df.apply(lambda x: f(x.col_1, x.col_2), axis=1)

This allows f to be a user-defined function with multiple input values, and uses (safe) column names rather than (unsafe) numeric indices to access the columns.

Example with data (based on original question):

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.apply(lambda x: get_sublist(x.col_1, x.col_2), axis=1)

Output of print(df):

  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]

If your column names contain spaces or share a name with an existing dataframe attribute, you can index with square brackets:

df['col_3'] = df.apply(lambda x: f(x['col 1'], x['col 2']), axis=1)
2 of 16
483

Here's an example using apply on the dataframe, which I am calling with axis = 1.

Note the difference is that instead of trying to pass two values to the function f, rewrite the function to accept 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 on the group.

๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-apply-a-function-with-multiple-arguments-to-create-a-new-pandas-column
How to Apply a Function with Multiple Arguments to Create a New Pandas Column | Saturn Cloud Blog
October 4, 2023 - This function returns the result of calling add_columns() with the values of columns A and B from the current row of the DataFrame. We specified axis=1 to apply the function to each row of the DataFrame. In this article, we have explored how to apply a function with multiple arguments to create a new pandas column.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Use apply() function to Return Multiple Columns in Pandas DataFrame - YouTube
This tutorial demonstrated how to generate separate multiple Pandas DataFrame columns from single column. For example, if you have name and surname in one fe...
Published ย  October 21, 2021