5 Practical Ways to Use IF Conditions in a Pandas DataFrame

In this guide, you’ll learn five different techniques to apply an IF condition in a Pandas DataFrame. These approaches are commonly used when transforming, cleaning, or categorizing data.

Specifically, you’ll see how to apply an IF condition for:

  • A set of numbers
  • A set of numbers using lambda
  • Strings
  • Strings using lambda
  • An OR condition

You’ll also see how to update values inside an existing column.

Applying an IF Condition in Pandas DataFrame

Let’s walk through each scenario step by step.

(1) IF Condition – Set of Numbers

Suppose you create a DataFrame containing numbers from 1 to 10. You want to apply the following logic:

  • If the number is less than or equal to 4 → assign “Yes”
  • Otherwise → assign “No”

The general structure using .loc is:

df.loc[df['column_name'] condition, 'new_column'] = 'value'

Example:

import pandas as pd

data = {'numbers': [1,2,3,4,5,6,7,8,9,10]}
df = pd.DataFrame(data)

df.loc[df['numbers'] <= 4, 'is_small'] = 'Yes'
df.loc[df['numbers'] > 4, 'is_small'] = 'No'

print(df)

Output:

   numbers is_small
0        1      Yes
1        2      Yes
2        3      Yes
3        4      Yes
4        5       No
5        6       No
6        7       No
7        8       No
8        9       No
9       10       No

(2) IF Condition – Set of Numbers Using Lambda

You can achieve the same result using apply() with a lambda function.

Generic structure:

df['new_column'] = df['column_name'].apply(lambda x: value_if_true if condition else value_if_false)

Example:

import pandas as pd

data = {'numbers': [1,2,3,4,5,6,7,8,9,10]}
df = pd.DataFrame(data)

df['is_small'] = df['numbers'].apply(lambda x: 'Yes' if x <= 4 else 'No')

print(df)

This produces the same output as in Case 1.

(3) IF Condition – Strings

Now consider a DataFrame that contains names:

  • If the name equals “David” → assign “Match”
  • Otherwise → assign “Mismatch”
import pandas as pd

data = {'first_name': ['John', 'David', 'Anna', 'Sophia']}
df = pd.DataFrame(data)

df.loc[df['first_name'] == 'David', 'name_check'] = 'Match'
df.loc[df['first_name'] != 'David', 'name_check'] = 'Mismatch'

print(df)

Output:

  first_name name_check
0       John   Mismatch
1      David      Match
2       Anna   Mismatch
3     Sophia   Mismatch

(4) IF Condition – Strings Using Lambda

The same logic can be written using lambda:

import pandas as pd

data = {'first_name': ['John', 'David', 'Anna', 'Sophia']}
df = pd.DataFrame(data)

df['name_check'] = df['first_name'].apply(lambda x: 'Match' if x == 'David' else 'Mismatch')

print(df)

The result is identical to Case 3.

(5) IF Condition with OR

Now suppose you want:

  • If the name is “David” or “Sophia” → assign “Match”
  • Otherwise → assign “Mismatch”
import pandas as pd

data = {'first_name': ['John', 'David', 'Anna', 'Sophia']}
df = pd.DataFrame(data)

df.loc[(df['first_name'] == 'David') | (df['first_name'] == 'Sophia'), 'name_check'] = 'Match'
df.loc[(df['first_name'] != 'David') & (df['first_name'] != 'Sophia'), 'name_check'] = 'Mismatch'

print(df)

Output:

  first_name name_check
0       John   Mismatch
1      David      Match
2       Anna   Mismatch
3     Sophia      Match

Notice that:

  • | represents OR
  • & represents AND
  • Each condition must be wrapped in parentheses

Applying an IF Condition to an Existing Column

So far, you created new columns. Now let’s modify values inside an existing column.

Example 1: Replace Specific Values

Assume the DataFrame contains:

[1,2,3,4,5,6,7,8,9,10,0,0]

Rules:

  • Replace 0 with 999
  • Replace 5 with 555
import pandas as pd

data = {'numbers': [1,2,3,4,5,6,7,8,9,10,0,0]}
df = pd.DataFrame(data)

print("BEFORE:")
print(df)

df.loc[df['numbers'] == 0, 'numbers'] = 999
df.loc[df['numbers'] == 5, 'numbers'] = 555

print("AFTER:")
print(df)

Output:

BEFORE:
numbers
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 0
11 0
AFTER:
numbers
0 1
1 2
2 3
3 4
4 555
5 6
6 7
7 8
8 9
9 10
10 999
11 999

The value 5 becomes 555, and zeros become 999 directly within the same column.

Example 2: Replace NaN Values

If your DataFrame contains missing values (NaN), you can replace them conditionally.

import pandas as pd
import numpy as np

data = {'numbers': [1,2,3,4,5,6,7,8,9,10,np.nan,np.nan]}
df = pd.DataFrame(data)

print("BEFORE:")
print(df)

df.loc[df['numbers'].isnull(), 'numbers'] = 0

print("AFTER:")
print(df)

Before execution, you will see NaN values. After running the condition, those values are replaced with zeros.

Conclusion

You have now seen multiple ways to apply IF conditions in a Pandas DataFrame. Whether you use .loc, apply() with lambda, OR conditions, or modify existing columns, the result can often be achieved through different approaches.

The choice between methods depends on readability, performance, and personal preference. For simple conditions, .loc is often clear and efficient. For row-by-row logic, lambda may feel more intuitive.

Understanding these techniques gives you flexibility when transforming and cleaning data in real-world projects.

See also: How to Convert Pandas DataFrame into a List

Vinish Kapoor
Vinish Kapoor

An Oracle ACE and software veteran with 25+ years of experience, passionate about AI and IT innovation.

guest

0 Comments
Oldest
Newest Most Voted