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 - The task is to create a new column “New Var” that contains the result of multiplying the age and fare of the individual if its a male and same variable stores the result of adding the age of the individual with their fare if female. While there might be multiple ways to achieve this, we’ll be making use of pandas apply function.
Discussions

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 - 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
Append multiple columns applying function that use multiple columns as attr (Pandas)
Your function returns a tuple, which you're trying to assign to more than one column (a tuple is a single object). For example, running something like df['c'] = df.apply(lambda x: f(x.a,x.b), axis=1) would return your tuple within one column. If you wanted just the first operation of your function you would grab the first value in the tuple df['c'] = df.apply(lambda x: f(x.a,x.b)[0], axis=1) And if you wanted to assign the output to two columns within one statement, you can just unpack the tuple in your lambda expression by converting the tuple to a Series. df[['c','d']] = df.apply(lambda x: pd.Series(f(x.a,x.b)), axis=1) More on reddit.com
🌐 r/learnpython
3
2
June 27, 2022
[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
June 21, 2022
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.

🌐
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.
🌐
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 ...
🌐
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 ...
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
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)
🌐
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.
🌐
ActiveState
activestate.com › home › resources › quick read › how to apply functions in pandas
How to Apply Functions in Pandas - ActiveState
January 24, 2024 - Click to apply functions in Pandas library. Apply logic, reduction or functions from NumPy using multiple values from multiple columns.
🌐
Medium
deallen7.medium.com › how-to-apply-a-function-with-two-or-more-arguments-in-a-pandas-dataframe-ea80abe68976
How to Apply a Function with Two (or more) Arguments in a Pandas DataFrame | by David Allen | Medium
August 16, 2023 - The approaches are somewhat different, using the .apply() method directly on a column to apply a single-parameter function, and then using a lambda function to pass multiple parameters to a slightly more complex function.
🌐
Reddit
reddit.com › r/learnpython › append multiple columns applying function that use multiple columns as attr (pandas)
r/learnpython on Reddit: Append multiple columns applying function that use multiple columns as attr (Pandas)
June 27, 2022 -

I have a DataFrame and a function, and I'd like to append 'c', 'd' col using a,b passed into function.

df = pd.DataFrame({
    'a' : [1,2,3],
    'b' : [4,5,6],})

def f(a,b):
    return a+b, a-b

# What I assumed it should work, it did not.
df[['c', 'd']] = df.apply(lambda x: f(x.a, x.b), axis=1)
>>> ValueError: Columns must be same length as key

I know several ways that could make it work but it seems pretty hard-coded. I wonder how is the above not working and if it's possible to fix it, using apply method.

🌐
DataScientYst
datascientyst.com › apply-function-multiple-columns-pandas
How to apply function to multiple columns in Pandas
September 11, 2021 - You can use the following code to apply a function to multiple columns in a Pandas DataFrame: def get_date_time(row, date, time): return row[date] + ' ' +row[time] df.apply(get_date_time, axis=1, date='Date', time='Time') For applying function ...