Alternatively, you can use loc:

import pandas as pd

df = pd.DataFrame({"age": [-100, 300, 400, 500, 600, 700]})
df["age"].loc[(df["age"] < 500) & (df["age"] >= 0)] = 0

Now your df looks like this:

    age
0   -100
1   0
2   0
3   500
4   600
5   700
Answer from Jonathan on Stack Exchange
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ using-apply-in-pandas-lambda-functions-with-multiple-if-statements
Using Apply in Pandas Lambda functions with multiple if statements - GeeksforGeeks
June 20, 2025 - While the lambda function is good for simple conditions, it struggles with multiple if-elif-else logic. To overcome this, we can define a custom function and use apply() to handle more complex branching logic.
Discussions

How to write a lambda function that is conditional on two variables (columns) in python - Stack Overflow
@seeiespi You originally asked for "How to create a lambda function that takes two arguments?" -- This is how :) -- EdChum provided you with an answer that is more aligned with what you're intentions are/were with your dataset(s) and pandas. 2014-07-18T00:02:08.877Z+00:00 ... There's now an pretty easy way to do this. Just use apply ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Using lambda if condition on different columns in Pandas dataframe - Stack Overflow
0 Using lambda IF condition on columns in Pandas to extract and replace strings from a data frame comparing to a list More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Conditional Logic on Pandas DataFrame - Stack Overflow
How to apply conditional logic to a Pandas DataFrame. See DataFrame shown below, data desired_output 0 1 False 1 2 False 2 3 True 3 4 Tru... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Applying multiple filters to a list.
[x for x in L if f1(x) and f2(x)]? More on reddit.com
๐ŸŒ r/Python
9
9
September 1, 2017
๐ŸŒ
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 ... Pandas to apply conditional logic to data. This can be done by using nested if-else statements within the lambda function....
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.apply.html
pandas.DataFrame.apply โ€” pandas 3.0.1 documentation
>>> 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. In general, JIT compilation is only possible when the function passed to apply has type stability (variables in the function do not change their type during the execution).
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ most powerful python functions apply() and lambda()
Most powerful Python Functions apply() and lambda() - Analytics Vidhya
October 19, 2024 - Because you just need to care about the custom function, you should be able to design pretty much any logic with apply/lambda. Filtering and subsetting data frames are simple with Pandas. Normal operators and &,|, operators can be used to filter and subset data frames. # Single condition: dataframe with all movies rated greater than 8 df_gt_8 = df[df['Rating']>8] df_gt_8.head() # Multiple conditions: AND - dataframe with all movies rated greater than 8 and having more than 100000 votes And_df = df[(df['Rating']>8) & (df['Votes']>100000)] And_df.head()
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ using-lambda-function-pandas-to-set-column-values
Using Lambda Function Pandas to Set Column Values | Saturn Cloud Blog
January 25, 2024 - To set column values in a Pandas DataFrame, we can use the .apply() function along with a lambda function. The .apply() function applies a function to each element of a DataFrame.
Find elsewhere
๐ŸŒ
Data to Fish
datatofish.com โ€บ if-condition-in-pandas-dataframe
Two Ways to Apply an If-Condition on a pandas DataFrame
df.loc[df['column'] == condition_value, 'target_column' ] = then_value df['target_column'] = df['column'].apply(lambda x: then_value if x == condition_value)
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ using-apply-in-pandas-lambda-functions-with-multiple-if-statements
Using Apply in Pandas Lambda Functions with Multiple If Statements | Saturn Cloud Blog
October 30, 2025 - Here is an example of using apply with a lambda function that has multiple if statements to create a new column in a DataFrame: โš  This code is experimental content and was generated by AI. Please refer to this code as experimental only since we cannot currently guarantee its validity ยท import pandas as pd data = {'A': [1, -2, 3], 'B': [-4, 5, -6]} df = pd.DataFrame(data) df['sign'] = df['A'].apply(lambda x: 'positive' if x > 0 else 'negative' if x < 0 else 'zero') print(df)
๐ŸŒ
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 - # Apply function NumPy.square() ... # A B C #0 9 25 7 #1 4 16 6 #2 25 64 9 ... apply() is a Pandas DataFrame method used to apply a function along the axis of a DataFrame....
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ 5 ways to apply if-else conditional statements in pandas
5 Ways to Apply If-Else Conditional Statements in Pandas | Towards Data Science
January 28, 2025 - df['new column name'] = df['column name'].apply(lambda x: 'value if condition is true' if x condition else 'value if condition is false') For our specific example, the code would look like this: df['visits_category'] = df['visits_30days'].a...
๐ŸŒ
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 used the conditional statement inside the lambda function in the following example. We applied the condition on the Monthly Income column. If the monthly income is greater and equal to 5000, add Stable inside the Category column; otherwise, add UnStable. ... import pandas as pd df = pd.DataFrame( { "ID": [1, 2, 3, 4, 5], "Names": ["Samreena", "Asif", "Mirha", "Affan", "Mahwish"], "Age": [20, 25, 15, 10, 30], "Monthly Income": [4000, 6000, 5000, 2000, 8000], } ) df["Category"] = df["Monthly Income"].apply( lambda x: "Stable" if x >= 5000 else "UnStable" ) print(df)
๐ŸŒ
Pandas How To
pandashowto.com โ€บ pandas how to โ€บ data transformation โ€บ pandas apply: transform data with functions complete guide โ€ข pandas how to
Pandas Apply: Transform Data With Functions Complete Guide โ€ข Pandas How To
January 28, 2026 - # Create 'Senior' column based on Age df['Senior'] = df.apply(lambda row: row['Age'] > 35, axis=1) # Create 'Status' based on multiple conditions df['Status'] = df.apply( lambda row: 'High Earner' if row['Salary'] > 60000 else 'Standard', axis=1 ) print(df) Output: Name Age Salary Senior Status 0 Alice 25 50000 False Standard 1 Bob 30 75000 False High Earner 2 Charlie 35 60000 False High Earner 3 David 40 55000 True Standard ยท
๐ŸŒ
Medium
codeforests.medium.com โ€บ ppicpandas-tricks-pass-multiple-columns-to-lambda-e0c16312fb50
Pandas Tricks โ€” Pass Multiple Columns To Lambda | by codeforests | Jul, 2020 | Medium | Medium
December 15, 2021 - This article will be sharing with you how to pass multiple columns to lambda or self-defined functions when dealing with data using pandas
๐ŸŒ
Kanoki
kanoki.org โ€บ 2022 โ€บ 09 โ€บ 12 โ€บ python-lambda-if-else-elif-with-multiple-conditions-and-filter-list-with-conditions-in-lambda
python lambda if, else & elif with multiple conditions and filter list with conditions in lambda | kanoki
September 12, 2022 - We could also use lambda to loop over a list and evaluate a condition ยท Letโ€™s define a function with all the conditions that we want to evaluate ยท def f(grade): if grade > 7: return 'A+' elif grade >= 5 and grade <= 7: return 'A' elif grade <5: return 'B' else: return False ... DataFrames are a powerful tool for working with data in Python, and Pandas ...
๐ŸŒ
Statology
statology.org โ€บ home โ€บ pandas: how to use apply & lambda together
Pandas: How to Use Apply & Lambda Together
June 23, 2022 - df['col'] = df['col'].apply(lambda x: 'value1' if x < 20 else 'value2') The following examples show how to use this syntax in practice with the following pandas DataFrame:
๐ŸŒ
CSDN
devpress.csdn.net โ€บ python โ€บ 63046073c67703293080c104.html
Using Apply in Pandas Lambda functions with multiple if statements_python_Mangs-Python
2 weeks ago - df['Classification']=df['Size'].apply(lambda x: "<1m" if x<1000000 else "1-10m" if 1000000<x<10000000 else ...) I checked a few posts regarding multiple ifs in a lambda function, here is an example link, but that synthax is not working for me for some reason in a multiple ifs statement, but it was working in a single if condition.