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

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:

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

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['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)

This 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 with 4 names: Jon, Bill, Maria, 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 ‘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 see:

IF condition in pandas DataFrame

(4) IF condition – strings and lambada 

You’ll 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, 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 ‘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 get the following result:

if logic python

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 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.