Skip to content
Related Articles

Related Articles

Ways to apply an if condition in Pandas DataFrame
  • Last Updated : 10 Jul, 2020

Let’s discuss the different ways of applying If condition to a data frame in pandas.

1) Applying IF condition on Numbers
Let us create a Pandas DataFrame that has 5 numbers (say from 51 to 55). Let us apply IF conditions for the following situation. If the particular number is equal or lower than 53, then assign the value of ‘True’. Otherwise, if the number is greater than 53, then assign the value of ‘False’

Syntax:

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

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

from pandas import DataFrame
  
numbers = {'mynumbers': [51, 52, 53, 54, 55]}
df = DataFrame(numbers, columns =['mynumbers'])
  
df.loc[df['mynumbers'] <= 53, '<= 53'] = 'True' 
df.loc[df['mynumbers'] > 53, '<= 53'] = 'False' 
  
df

chevron_right


Output:

2) Applying IF condition with lambda
Let us create a Pandas DataFrame that has 5 numbers (say from 51 to 55). Let us apply IF conditions for the following situation. If the particular number is equal or lower than 53, then assign the value of ‘True’. Otherwise, if the number is greater than 53, then assign the value of ‘False’.

Syntax:

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’)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

from pandas import DataFrame
  
numbers = {'mynumbers': [51, 52, 53, 54, 55]}
df = DataFrame(numbers, columns =['mynumbers'])
  
df['<= 53'] = df['mynumbers'].apply(lambda x: 'True' if x <= 53 else 'False')
  
print (df)

chevron_right


Output:

3) Applying IF condition on strings
We will deal with the DataFrame that contains only strings with 5 names: Hanah, Ria, Jay, Bholu, Sachin. The conditions are: If the name is equal to ‘Ria, ’ then assign the value of ‘Found’. Otherwise, if the name is not ‘Ria, ’ then assign the value of ‘Not Found’.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

from pandas import DataFrame
  
names = {'First_name': ['Hanah', 'Ria', 'Jay', 'Bholu', 'Sachin']}
df = DataFrame(names, columns =['First_name'])
  
df.loc[df['First_name'] == 'Ria', 'Status'] = 'Found'  
df.loc[df['First_name'] != 'Ria', 'Status'] = 'Not Found' 
  
print (df)

chevron_right


Output:

4) Applying IF condition on strings using lambada
We will deal with the DataFrame that contains only strings with 5 names: Hanah, Ria, Jay, Bholu, Sachin. The conditions are: If the name is equal to ‘Ria, ’ then assign the value of ‘Found’. Otherwise, if the name is not ‘Ria, ’ then assign the value of ‘Not Found’. But this time we will deal with it using lambdas.

Example

filter_none

edit
close

play_arrow

link
brightness_4
code

from pandas import DataFrame
  
names = {'First_name': ['Hanah', 'Ria', 'Jay', 'Bholu', 'Sachin']}
df = DataFrame(names, columns =['First_name'])
  
df['Status'] = df['First_name'].apply(lambda x: 'Found' if x == 'Ria' else 'Not Found')
  
print (df)

chevron_right


Output:

5) Applying IF condition with OR
We will deal with the DataFrame that contains only strings with 5 names: Hanah, Ria, Jay, Bholu, Sachin. The conditions are: If the name is equal to “Ria”, or “Jay” then assign the value of ‘Found’. Otherwise, if the name is not “Ria” or “Jay” then assign the value of ‘Not Found’.

Example

filter_none

edit
close

play_arrow

link
brightness_4
code

from pandas import DataFrame
  
names = {'First_name': ['Hanah', 'Ria', 'Jay', 'Bholu', 'Sachin']}
df = DataFrame(names, columns =['First_name'])
  
df.loc[(df['First_name'] == 'Ria') | (df['First_name'] == 'Jay'), 'Status'] = 'Found'  
df.loc[(df['First_name'] != 'Ria') & (df['First_name'] != 'Jay'), 'Status'] = 'Not Found'  
  
print (df)

chevron_right


Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :