Open In App

# Ways to apply an if condition in Pandas DataFrame

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

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

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

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

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

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

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

### Applying IF condition on strings using lambda

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

## Python3

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

Output:

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

## 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'``) | (df[``'First_name'``] ``=``=` `'Jay'``), ``'Status'``] ``=` `'Found'` `df.loc[(df[``'First_name'``] !``=` `'Ria'``) & (df[``'First_name'``] !``=` `'Jay'``), ``'Status'``] ``=` `'Not Found'`  `print` `(df)`

Output: