5 ways to apply an IF condition in Pandas DataFrame

In this guide, you’ll see 5 different ways to apply an IF condition in Pandas DataFrame.

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

  1. Set of numbers
  2. Set of numbers and lambda
  3. Strings
  4. Strings and lambada
  5. OR condition

Applying an IF condition in Pandas DataFrame

Let’s now review the following 5 cases:

(1) IF condition – Set of numbers

Suppose that you created a DataFrame in Python that has 10 numbers (from 1 to 10). You then want to apply the following IF conditions:

  • If the number is equal or lower than 4, then assign the value of ‘True’
  • Otherwise, if the number is greater than 4, then assign the value of ‘False’

This is the general structure that you may use to create the IF condition:

df.loc[df['column name'] condition, 'new column name'] = 'value if condition is met'

For our example, the Python code would look like this:

import pandas as pd

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

df.loc[df['set_of_numbers'] <= 4, 'equal_or_lower_than_4?'] = 'True' 
df.loc[df['set_of_numbers'] > 4, 'equal_or_lower_than_4?'] = 'False' 

print (df)

Here is the result that you’ll get in Python:

   set_of_numbers   equal_or_lower_than_4?
0               1                     True
1               2                     True
2               3                     True
3               4                     True
4               5                    False
5               6                    False
6               7                    False
7               8                    False
8               9                    False
9              10                    False

(2) IF condition – set of numbers and lambda

You’ll now see how to get the same results as in case 1 by using lambada, where the conditions are:

  • If the number is equal or lower than 4, then assign the value of ‘True’
  • Otherwise, if the number is greater than 4, then assign the value of ‘False’

Here is the generic structure that you may apply in Python:

df['new column name'] = df['column name'].apply(lambda x: 'value if condition is met' if x condition else 'value if condition is not met')

And for our example:

import pandas as pd

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

df['equal_or_lower_than_4?'] = df['set_of_numbers'].apply(lambda x: 'True' if x <= 4 else 'False')

print (df)

This is the result that you’ll get, which matches with case 1:

   set_of_numbers   equal_or_lower_than_4?
0               1                     True
1               2                     True
2               3                     True
3               4                     True
4               5                    False
5               6                    False
6               7                    False
7               8                    False
8               9                    False
9              10                    False

(3) IF condition – strings

Now, let’s create a DataFrame that contains only strings/text with 4 names: Jon, Bill, Maria and Emma.

The conditions are:

  • If the name is equal to ‘Bill,’ then assign the value of ‘Match’
  • Otherwise, if the name is not ‘Bill,’ then assign the value of ‘Mismatch’
import pandas as pd

names = {'first_name': ['Jon','Bill','Maria','Emma']}
df = pd.DataFrame(names,columns=['first_name'])

df.loc[df['first_name'] == 'Bill', 'name_match'] = 'Match'  
df.loc[df['first_name'] != 'Bill', 'name_match'] = 'Mismatch'  
 
print (df)

Once you run the above Python code, you’ll see:

  first_name   name_match
0        Jon     Mismatch
1       Bill        Match
2      Maria     Mismatch
3       Emma     Mismatch

(4) IF condition – strings and lambada 

You’ll get the same results as in case 3 by using lambada:

import pandas as pd

names = {'first_name': ['Jon','Bill','Maria','Emma']}
df = pd.DataFrame(names,columns=['first_name'])

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

print (df)

And here is the output from Python:

  first_name   name_match
0        Jon     Mismatch
1       Bill        Match
2      Maria     Mismatch
3       Emma     Mismatch

(5) IF condition with OR

In the final case, let’s apply these conditions:

  • If the name is ‘Bill’ or ‘Emma,’ then assign the value of ‘Match’
  • Otherwise, if the name is neither ‘Bill’ nor ‘Emma,’ then assign the value of ‘Mismatch’
import pandas as pd

names = {'first_name': ['Jon','Bill','Maria','Emma']}
df = pd.DataFrame(names,columns=['first_name'])

df.loc[(df['first_name'] == 'Bill') | (df['first_name'] == 'Emma'), 'name_match'] = 'Match'  
df.loc[(df['first_name'] != 'Bill') & (df['first_name'] != 'Emma'), 'name_match'] = 'Mismatch'  

print (df)

Run the Python code, and you’ll get the following result:

  first_name   name_match
0        Jon     Mismatch
1       Bill        Match
2      Maria     Mismatch
3       Emma        Match

Applying an IF condition under an existing DataFrame column

So far you have seen how to apply an IF condition by creating a new column.

Alternatively, you may store the results under an existing DataFrame column.

For example, let’s say that you created a DataFrame that has 12 numbers, where the last two numbers are zeros:

‘set_of_numbers’: [1,2,3,4,5,6,7,8,9,10,0,0]

You may then apply the following IF conditions, and then store the results under the existing ‘set_of_numbers’ column:

  • If the number is equal to 0, then change the value to 999
  • If the number is equal to 5, then change the value to 555
import pandas as pd

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

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

print (df)

Here are the before and after results, where the ‘5’ became ‘555’ and the 0’s became ‘999’ under the existing ‘set_of_numbers’ column:

BEFORE:

    set_of_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:

    set_of_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

On another instance, you may have a DataFrame that contains NaN values. You can then apply an IF condition to replace those values with zeros, as in the example below:

import pandas as pd
import numpy as np

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

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

Before you’ll see the NaN values, and after you’ll see the zero values:

BEFORE:

    set_of_numbers
0              1.0
1              2.0
2              3.0
3              4.0
4              5.0
5              6.0
6              7.0
7              8.0
8              9.0
9             10.0
10             NaN
11             NaN

AFTER:

    set_of_numbers
0              1.0
1              2.0
2              3.0
3              4.0
4              5.0
5              6.0
6              7.0
7              8.0
8              9.0
9             10.0
10             0.0
11             0.0

Conclusion

You just saw how to apply an IF condition in Pandas DataFrame. There are indeed multiple ways to apply such a condition in Python. You can achieve the same results by using either lambada, or just by sticking with Pandas.

At the end, it boils down to working with the method that is best suited to your needs.

Finally, you may want to check the following external source for additional information about Pandas DataFrame.