Open In App

Ways to apply an if condition in Pandas DataFrame

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Pandas provides several methods to apply the if condition to a DataFrame, and the choice of method depends on the complexity of the condition and the desired outcome. In this article, we will explore various ways of applying the if condition to a DataFrame in Pandas.

Apply an If Condition in Pandas DataFrame

There are various ways to use if/truth statements with pandas. here we are discussing some generally used methods for Using if/truth statements with pandas those are following.

  • Applying IF condition Using pd.where Method
  • Applying IF Condition on Numbers 
  • Applying IF Condition On Strings 
  • Applying IF Condition with Lambda  

Applying IF condition Using pd.where Method

In this example, the below code uses Pandas to create a DataFrame with a ‘Numbers’ column. It applies an if condition using the where method, replacing values below a threshold with ‘Below Threshold’ in a new column ‘Conditioned_Numbers’.

Python3




import pandas as pd
 
# Sample DataFrame
data = {'Numbers': [1, 5, 8, 3, 6, 9]}
df = pd.DataFrame(data)
 
# Applying an if condition using where method
threshold = 5
df['Conditioned_Numbers'] = df['Numbers'].where(df['Numbers'] > threshold, other='Below Threshold')
 
# Display the DataFrame
print(df)


Output:

   Numbers Conditioned_Numbers
0        1     Below Threshold
1        5     Below Threshold
2        8                   8
3        3     Below Threshold
4        6                   6
5        9                   9

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

In this example, code creates a pandas DataFrame named ‘df’ with a column ‘mynumbers‘ containing [51, 52, 53, 54, 55]. It then adds a new column ‘<= 53’ and assigns ‘True’ if ‘mynumbers’ is less than or equal to 53, and ‘False’ if it’s greater than 53. The resulting DataFrame is displayed.

Python3




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


Output: 

    mynumbers    <= 53
0    51    True
1    52    True
2    53    True
3    54    False
4    55    False

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

In this example code creates a pandas DataFrame named df with a column ‘First_name’ containing a list of names. It adds a new column ‘Status’ and assigns ‘Found’ if the name is ‘Ria’ and ‘Not Found’ otherwise. The resulting DataFrame is then printed.

Python3




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)


Output: 

First_name     Status
0      Hanah  Not Found
1        Ria      Found
2        Jay  Not Found
3      Bholu  Not Found
4     Sachin  Not Found

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

In this example code creates a Pandas DataFrame named ‘df’ with a column ‘mynumbers’ containing a list of integers. It then adds a new column ‘<= 53’ based on a conditional lambda function, indicating whether each value in ‘mynumbers’ is less than or equal to 53. Finally, it prints the resulting DataFrame.

Python3




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)


Output: 

 mynumbers  <= 53
0         51   True
1         52   True
2         53   True
3         54  False
4         55  False



Last Updated : 17 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads