If you only have two choices to select from then use np.where:

df['color'] = np.where(df['Set']=='Z', 'green', 'red')

For example,

import pandas as pd
import numpy as np

df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
df['color'] = np.where(df['Set']=='Z', 'green', 'red')
print(df)

yields

  Set Type  color
0   Z    A  green
1   Z    B  green
2   X    B    red
3   Y    C    red

If you have more than two conditions then use np.select. For example, if you want color to be

  • yellow when (df['Set'] == 'Z') & (df['Type'] == 'A')
  • otherwise blue when (df['Set'] == 'Z') & (df['Type'] == 'B')
  • otherwise purple when (df['Type'] == 'B')
  • otherwise black,

then use

df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
conditions = [
    (df['Set'] == 'Z') & (df['Type'] == 'A'),
    (df['Set'] == 'Z') & (df['Type'] == 'B'),
    (df['Type'] == 'B')]
choices = ['yellow', 'blue', 'purple']
df['color'] = np.select(conditions, choices, default='black')
print(df)

which yields

  Set Type   color
0   Z    A  yellow
1   Z    B    blue
2   X    B  purple
3   Y    C   black
Answer from unutbu on Stack Overflow
🌐
Dataquest
dataquest.io › blog › tutorial-add-column-pandas-dataframe-based-on-if-else-condition
Add a Column in a Pandas DataFrame Based on an If-Else Condition
March 6, 2023 - To accomplish this, we’ll use numpy’s built-in where() function. This function takes three arguments in sequence: the condition we’re testing for, the value to assign to our new column if that condition is true, and the value to assign if it is false.
🌐
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')
🌐
Medium
medium.com › @amit25173 › simple-ways-to-apply-if-else-condition-in-pandas-dataframe-2331b8843daa
Simple Ways to Apply if-Else Condition in Pandas DataFrame | by Amit Yadav | Medium
April 12, 2025 - You can create complex functions that evaluate multiple columns, or use apply() with axis=1 to operate row-wise. Will using if-else statements slow down my code? For small datasets, if-else conditions perform efficiently. However, for large DataFrames, consider vectorized operations with NumPy for better performance. Can I chain if-else statements in Pandas...
🌐
Statology
statology.org › home › pandas: create new column using multiple if else conditions
Pandas: Create New Column Using Multiple If Else Conditions
October 10, 2022 - This tutorial explains how to create a new column in a pandas DataFrame using multiple if else conditions, including an example.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-use-ifelse-function-in-pandas-dataframe
How to Use If-Else Function in Pandas DataFrame | Saturn Cloud Blog
November 10, 2023 - Let’s take an example to understand how the if-else function works in Pandas DataFrame. Suppose you have a DataFrame with two columns, 'Name' and 'Score', and you want to add a third column 'Result', which will contain the value 'Pass' if the score is greater than or equal to 50 and 'Fail' otherwise.
Top answer
1 of 15
1062

If you only have two choices to select from then use np.where:

df['color'] = np.where(df['Set']=='Z', 'green', 'red')

For example,

import pandas as pd
import numpy as np

df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
df['color'] = np.where(df['Set']=='Z', 'green', 'red')
print(df)

yields

  Set Type  color
0   Z    A  green
1   Z    B  green
2   X    B    red
3   Y    C    red

If you have more than two conditions then use np.select. For example, if you want color to be

  • yellow when (df['Set'] == 'Z') & (df['Type'] == 'A')
  • otherwise blue when (df['Set'] == 'Z') & (df['Type'] == 'B')
  • otherwise purple when (df['Type'] == 'B')
  • otherwise black,

then use

df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
conditions = [
    (df['Set'] == 'Z') & (df['Type'] == 'A'),
    (df['Set'] == 'Z') & (df['Type'] == 'B'),
    (df['Type'] == 'B')]
choices = ['yellow', 'blue', 'purple']
df['color'] = np.select(conditions, choices, default='black')
print(df)

which yields

  Set Type   color
0   Z    A  yellow
1   Z    B    blue
2   X    B  purple
3   Y    C   black
2 of 15
191

List comprehension is another way to create another column conditionally. If you are working with object dtypes in columns, like in your example, list comprehensions typically outperform most other methods.

Example list comprehension:

df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]

%timeit tests:

import pandas as pd
import numpy as np

df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
%timeit df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
%timeit df['color'] = np.where(df['Set']=='Z', 'green', 'red')
%timeit df['color'] = df.Set.map( lambda x: 'red' if x == 'Z' else 'green')

1000 loops, best of 3: 239 µs per loop
1000 loops, best of 3: 523 µs per loop
1000 loops, best of 3: 263 µs per loop
🌐
Data to Fish
datatofish.com › if-condition-in-pandas-dataframe
Two Ways to Apply an If-Condition on a pandas DataFrame
In this tutorial, you will learn two ways to apply an if condition a 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)
Find elsewhere
🌐
Guillim
guillim.github.io › pandas › 2018 › 10 › 22 › Pandas-if-else-on-columns.html
Pandas: if else on columns | 📕 Learn 💻 Build 🚀 Ship
October 22, 2018 - import pandas as pd, numpy as np df = pd.DataFrame(np.random.randint(low=0, high=10, size=(1000000)), columns=['column_1']) If you develop, you will intuitively use a row by row pattern, like this: new_results = {} for index, row in df.iterrows(): row["column_2"] = 'high' if row["column_1"] > 5 else 'low' if row["column_1"] > 0 else 'null' new_results[index] = dict(row) df = pd.DataFrame.from_dict(new_results, orient='index') it works.
🌐
Delft Stack
delftstack.com › home › howto › python pandas › pandas if else
How to Apply the If-Else Condition in a Pandas DataFrame | Delft Stack
February 2, 2024 - The function consists of if-else conditions that assign the result based on the Marks and invoke this for every column row. ... # Python 3.x import pandas as pd df = pd.DataFrame( {"Name": ["Robert", "Sam", "Alia", "Jhon", "Smith"], "Marks": ...
🌐
GeeksforGeeks
geeksforgeeks.org › ways-to-apply-an-if-condition-in-pandas-dataframe
How to apply if condition in Pandas DataFrame - GeeksforGeeks
November 21, 2024 - import pandas as pd # Sample DataFrame data = {'Name': ['John', 'Sophia', 'Daniel', 'Emma'], 'Experience': [5, 8, 3, 10]} df = pd.DataFrame(data) print("Original Dataset") display(df) # Apply if condition using lambda function df['Category'] = df['Experience'].apply(lambda x: 'Senior' if x >= 5 else 'Junior') print("Dataset with 'Senior'and 'Junior' Category") display(df) Output: Applying 'if condition' to classify the 'Experience' column into 'Senior' and 'Junior' categories ·
🌐
HubSpot
blog.hubspot.com › home › the hubspot website blog
Using If-Else Statements in Pandas: A Practical Guide ...
April 3, 2022 - HubSpot’s Website Blog covers everything you need to know to build maintain your company’s website.
🌐
Medium
medium.com › @amit25173 › implementing-pandas-if-else-new-column-319d7bb34f7c
Implementing Pandas if Else New Column | by Amit Yadav | Medium
April 12, 2025 - You can chain multiple conditions together using functions or by nesting the if-else statements in a single lambda function. How does the apply() function work in Pandas? apply() lets you apply a function along the axis of the DataFrame; it’s perfect for row-wise or column-wise operations.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas create conditional column in dataframe
Pandas Create Conditional Column in DataFrame - Spark By {Examples}
March 27, 2024 - # Replace values where the condition is True df['Discount'] = df['Discount'].mask(df['Courses']=='Spark', other=1000) # Using where() df['Discount'] = df['Discount'].where(df['Courses']=='Spark', other=1000) # Using transform() with a lambda function. df['Discount'] = df['Courses'].transform(lambda x: 1000 if x == 'Spark' else 2000) Let’s create a pandas DataFrame with a few rows and columns and execute these examples and validate results.
🌐
Medium
medium.com › swlh › adding-a-column-to-a-pandas-dataframe-based-on-an-if-else-condition-895cfc666548
Adding a Column to a Pandas DataFrame Based on an If-Else Condition | by Ken Hoffman | The Startup | Medium
January 8, 2021 - To do this, we would use the function, np.select(). We give it two arguments: a list of the conditions for the column and the corresponding list of values that we want to give each condition.
🌐
Medium
medium.com › @ODSC › creating-if-elseif-else-variables-in-python-pandas-7900f512f0e4
Creating if/elseif/else Variables in Python/Pandas | by ODSC - Open Data Science | Medium
March 4, 2020 - Summary: This blog demos Python/Pandas/Numpy code to manage the creation of Pandas dataframe attributes with if/then/else logic. It contrasts five approaches for conditional variables using a combination of Python, Numpy, and Pandas features/techniques.
🌐
Niche Capital
nichecapitalco.com › using-if-else-statements-in-pandas-a-practical-guide
Using If-Else Statements in Pandas: A Practical Guide
# Creating a sample DataFrame data = { 'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'], 'Age': [24, 27, 22, 32, 29], 'Score': [85, 62, 75, 90, 88] } df = pd.DataFrame(data) # Applying if-else logic to create a new column 'Pass' df['Pass'] = df['Score'].apply(lambda x: 'Yes' if x >= 70 else 'No') print(df) In the example above, we create a new column called ‘Pass’ which has a value ‘Yes’ if the score is greater than or equal to 70, and ‘No’ otherwise. This is achieved using the apply method along with a lambda function. NumPy’s where function is another efficient way to apply conditional logic in Pandas.