5 ways to apply an IF condition in pandas DataFrame

Need to apply an IF condition in pandas DataFrame?

If so, in this tutorial, I’ll show you 5 different ways to apply such a condition.

Specifically, I’ll show you 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

Before we dive into those 5 scenarios, make sure that the pandas package is installed in Python.

Applying an IF condition in Pandas DataFrame

Let’s now review the following 5 cases:

(1) IF condition – Set of numbers

Let’s say that you created a DataFrame in Python that has 10 numbers (from 1 to 10). You would then like to apply the following IF condition:

  • If the number is equal or lower than 4, then assign the value of ‘True’
  • While 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'

 

And for our example, the Python code would look like this:

 

from pandas import DataFrame

Numbers = {'set_of_numbers': [1,2,3,4,5,6,7,8,9,10]}
df = 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:

 

5 ways to apply an IF condition in pandas DataFrame

(2) IF condition – set of numbers and lambda

We will 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’
  • While 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['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:

 

from pandas import DataFrame

Numbers = {'set_of_numbers': [1,2,3,4,5,6,7,8,9,10]}
df = 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)

 

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

 

5 ways to apply an IF condition in pandas DataFrame

(3) IF condition – strings

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

The conditions that we will use are:

  • If the name is equal to ‘Bill,’ then assign the value of ‘Match’
  • While if the name is not ‘Bill,’  then assign the value of ‘Mis-Match’

 

from pandas import DataFrame

Names1 = {'First_name': ['Jon','Bill','Maria','Emma']}
df = DataFrame(Names1,columns=['First_name'])

df.loc[df.First_name == 'Bill', 'name_match'] = 'Match'  
df.loc[df.First_name != 'Bill', 'name_match'] = 'Mis-Match'  
 
print (df)

 

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

 

IF condition in pandas DataFrame

(4) IF condition – strings and lambada 

We will get the same results as in case 3 by using lambada:

 

from pandas import DataFrame

Names1 = {'First_name': ['Jon','Bill','Maria','Emma']}
df = DataFrame(Names1,columns=['First_name'])

df['name_match'] = df['First_name'].apply(lambda x: 'match' if x == 'Bill' else 'Mis-match')

print (df)

 

And here is the output from Python:

 

IF condition in pandas DataFrame

(5) IF condition with OR

In the final case, we will 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 ‘Mis-Match’

 

from pandas import DataFrame

Names1 = {'First_name': ['Jon','Bill','Maria','Emma']}
df = DataFrame(Names1,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'] = 'Mis-Match'  

print (df)

 

Run the Python code, and you’ll see the following:

 

IF condition in pandas Python

Conclusion

We 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 sticking with the DataFrame.

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.