Given a sample dataframe df as:

   a  b
0  1  2
1  2  3
2  3  4
3  4  5

what you want is:

df['a'] = df['a'].apply(lambda x: x + 1)

that returns:

   a  b
0  2  2
1  3  3
2  4  4
3  5  5
Answer from Fabio Lamanna on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › applying-lambda-functions-to-pandas-dataframe
Applying Lambda functions to Pandas Dataframe - GeeksforGeeks
July 15, 2025 - The function is applied to the 'Total_Marks' column, and a new column 'Percentage' is formed with its help. ... # importing pandas library import pandas as pd # creating and initializing a list values= [['Rohan',455],['Elvish',250],['Deepak',495], ...
Discussions

How to properly apply a lambda function into a pandas data frame column - Stack Overflow
I have a pandas data frame, sample, with one of the columns called PR to which am applying a lambda function as follows: sample['PR'] = sample['PR'].apply(lambda x: NaN if x More on stackoverflow.com
🌐 stackoverflow.com
How to apply a lambda on pandas DataFrame column headers
You can pass multiple column names to get a "slice" >>> dfa[['BZ_Clicks', 'MF_Clicks']] BZ_Clicks MF_Clicks 0 40 50 1 50 30 2 60 120 [3 rows x 2 columns] This means you can pass in a comprehension directly >>> dfa[[col for col in dfa.columns if 'PX' in col]] BZ_PX_Joins MF_PX_Joins 0 10 5 1 30 12 2 25 25 [3 rows x 2 columns] More on reddit.com
🌐 r/learnpython
3
2
December 29, 2018
python - How to apply lambda function to specific column based on the values in the adjacent column - Stack Overflow
I am trying a apply a lambda function to a pandas data frame. My question is how can I apply a lambda function to column a based on value in column b using if statement. A B C 2 5 7 4 5 9 6 7 9 df... More on stackoverflow.com
🌐 stackoverflow.com
Optimal way to apply functions for different columns in pandas
You need to reformat your data. It should be {column: [values], column2: [values]} and then it’s just pd.DataFrame(mydict) Doing it your way is not scalable because you’re hardcoding the column names. If you ever add or remove columns you’re going to get errors. More on reddit.com
🌐 r/learnpython
2
0
January 30, 2023
🌐
ProjectPro
projectpro.io › blog › how to apply lambda functions to python pandas?
How To Apply Lambda Functions To Python Pandas?
October 28, 2024 - For example, you can use the following code to apply a lambda function to a DataFrame to double all the values in the 'Age' column for each row- ... You can apply Lambda functions to multiple rows in Pandas using the .apply() method.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.apply.html
pandas.DataFrame.apply — pandas 3.0.6 documentation
Passing result_type='broadcast' will ensure the same shape result, whether list-like or scalar is returned by the function, and broadcast it along the axis. The resulting column names will be the originals. >>> df.apply(lambda x: [1, 2], axis=1, result_type="broadcast") A B 0 1 2 1 1 2 2 1 2 · Advanced users can speed up their code by using a Just-in-time (JIT) compiler with apply. The main JIT compilers available for pandas are Numba and Bodo.
🌐
Statology
statology.org › home › pandas: how to use apply & lambda together
Pandas: How to Use Apply & Lambda Together
June 23, 2022 - You can use the following basic syntax to apply a lambda function to a pandas DataFrame:
🌐
Delft Stack
delftstack.com › home › howto › python pandas › apply lambda functions to pandas dataframe
How to Apply Lambda Function to Pandas DataFrame | Delft Stack
February 2, 2024 - We applied a Lambda function on multiple subjects columns such as Computer, Math, and Physics to calculate the obtained marks stored in the Marks_Obtained column. Implement the following example. ... import pandas as pd # nested list initialization values_list = [ ["Samreena", 85, 75, 100], ["Mehwish", 90, 75, 90], ["Asif", 95, 82, 80], ["Mirha", 75, 88, 68], ["Affan", 80, 63, 70], ["Raees", 91, 64, 90], ] # pandas dataframe creation df = pd.DataFrame(values_list, columns=["Student Names", "Computer", "Math", "Physics"]) # applying Lambda function dataframe = df.assign( Marks_Obtained=lambda x: (x["Computer"] + x["Math"] + x["Physics"]) ) # display dataframe print(dataframe)
🌐
Medium
deallen7.medium.com › how-to-apply-lambda-apply-function-in-a-pandas-dataframe-a6bf5c74dc1c
How to Apply Lambda & Apply Function in a Pandas Dataframe | by David Allen | Medium
July 26, 2022 - ... The core learning here is to use cell magic and measure the execution time! It’s a terrific way to evaluate your code performance: ... Strategy 1: Write a function, and apply that function.
Find elsewhere
🌐
Saturn Cloud
saturncloud.io › blog › using-lambda-function-pandas-to-set-column-values
Using Lambda Function Pandas to Set Column Values | Saturn Cloud Blog
May 1, 2026 - A lambda function can be used as an argument for other functions or used to create a new function on the fly. To set column values in a Pandas DataFrame, we can use the .apply() function along with a lambda function.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas apply() with lambda examples
Pandas apply() with Lambda Examples - Spark By {Examples}
June 17, 2025 - pandas.DataFrame.apply() can be used along with the Python lambda function to apply a custom operation to all columns in a DataFrame. A lambda function is
🌐
Analytics Vidhya
analyticsvidhya.com › home › learn how to use lambda functions in python easily and effectively
Learn How to Use Lambda Functions in Python Easily and Effectively
December 1, 2024 - We can do this with the apply() function in Pandas. apply() function calls the lambda function and applies it to every row or column of the dataframe and returns a modified copy of the dataframe:
🌐
Medium
infiniteknowledge.medium.com › how-to-apply-a-lambda-function-to-pandas-dataframe-3f97eb0cd556
How to apply a lambda function to pandas DataFrame? | by Panisetti Prudhviraj | Medium
February 6, 2023 - It’s often used to simplify code and make it more readable, and can be especially useful when working with data in a Pandas DataFrame. To apply a lambda function to a DataFrame, you can use the apply method. This method applies a function ...
🌐
The Carpentries
carpentries-incubator.github.io › python-business › 09-data_prep › index.html
Python for Business: Data Preparation techniques
June 19, 2020 - apply function will apply the lambda function on each value in the column, then return a pandas Series with return value of the lambda function.
🌐
Medium
medium.com › @manishsingh7163 › use-lambda-functions-in-pandas-dataframes-for-efficient-data-processing-5800184d9415
Use Lambda Functions in Pandas DataFrames for Efficient Data Processing | by Manish Singh | Medium
January 1, 2023 - For example, let’s say you have a DataFrame with a column of temperatures in Fahrenheit, and you want to convert them to Celsius. Here’s how you could do it using a lambda function: import pandas as pd # Create a sample DataFrame df = pd.DataFrame({'temperature (F)': [32, 212, 98.6, 68]}) # Use a lambda function to convert temperatures from Fahrenheit to Celsius df['temperature (C)'] = df['temperature (F)'].apply(lambda x: (x - 32) * 5/9) # Print the resulting DataFrame print(df)
🌐
Reddit
reddit.com › r/learnpython › how to apply a lambda on pandas dataframe column headers
r/learnpython on Reddit: How to apply a lambda on pandas DataFrame column headers
December 29, 2018 -

Hi experts,

Basically what I want to do is to allow the users to get whatever columns they want with a lambda function. I tried a lot of methods but none of them worked.

e.g. Let's say I have the following code:

dfa = pd.DataFrame({"BZ_Impressions": [1000, 2000, 3000],
                    "BZ_Clicks": [40, 50, 60], 
                    "MF_Impressions": [500, 600, 1200], 
                    "MZ_Clicks": [50, 30, 120], 
                    "BZ_PX_Joins": [10,30,25], 
                    "MF_PX_Joins": [5, 12, 25]})

I could, for example, only want columns that start with "BZ", and I could also maybe only want columns that has "Impressions" in it. I could also want columns that have a length smaller than 10 characters. The possibilities are too many so a lambda function is the only solution. But I'm willing to focus only on string functions on headers only (i.e. user cannot select columns based on actual data, only on headers)

I have tried to convert the column headers into a list and then apply a lambda function on it, but it says "list has no attribution 'apply'". I also tried numpy arrays but it didn't work. I'm wondering if I can directly apply something on DataFrame.columns so the users may get a sub-DataFrame easily?

Thank you!

🌐
Towards Data Science
towardsdatascience.com › home › latest › how to apply a function to columns in pandas
How To Apply a Function To Columns in Pandas | Towards Data Science
January 20, 2025 - On the other hand, in occasions where you need to apply a certain function over multiple columns, then you should probably use [pandas.DataFrame.apply()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html) method. For instance, let's suppose we need to apply the lambda function lambda x: x + 1 over the columns colA and colD.
🌐
GeeksforGeeks
geeksforgeeks.org › python › apply-a-function-to-each-row-or-column-in-dataframe-using-pandas-apply
Apply a function to each row or column in Dataframe using pandas.apply() - GeeksforGeeks
July 15, 2025 - In this example, we’ll apply a lambda function that adds 10 to each value in every column of the DataFrame. ... import pandas as pd import numpy as np # Creating a list of tuples for the DataFrame matrix = [(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16)] # Creating the DataFrame df = pd.DataFrame(matrix, columns=list('abcd')) # Output the DataFrame print("Original DataFrame:") print(df) print('\n') # Applying lambda function to add 10 to each value in every column new_df = df.apply(lambda x: x + 10) print("New DataFrame:") print(new_df)
🌐
Medium
medium.com › @manishsingh7163 › useful-trick-for-data-scientists-using-apply-and-lambda-function-288a583797af
Useful trick for data scientists using apply and lambda function | by Manish Singh | Medium
January 7, 2023 - All you have to do is specify which axis you want to apply the function to (rows or columns) and the function will be applied to each element on that axis. import pandas as pd # Define the lambda function lambda_function = lambda x: x['col1'] ...
🌐
TidyStat
tidystat.com › home › how to use lambda functions in python (pandas)
How to Use Lambda Functions in Python (Pandas)
January 20, 2025 - car_data["Year_old"]=car_data.... 1 Tesla CA 2018 4 2 Tesla NY 2020 2 3 Ford MA 2019 3 · You can use the combination of filter(), list(), and lambda functions to find the elements within a column in Pandas....