Python Pandas

Pandas And Condition

Pandas, just like other data analysis libraries, can also be used to perform data manipulation tasks using various modules and methods. We can also apply single or multiple conditions on Pandas data using Python operators. For example, to filter data based on multiple conditions simultaneously, the and “&” operator is used in Python.

This write-up presented a detailed tutorial on Pandas and “&” conditions using numerous examples via the following content:

What is the and “&” Condition in Python Pandas?

In Python, the and “&” operator is referred to as a logical operator that is utilized to combine/merge two or more conditions. This operator checks or verifies whether all the conditions are “True”. It retrieves “True” if all the particular conditions are “True” and False otherwise.

Syntax

dataframe[(condition_1) & (condition_2)]

In the above syntax, the “condition_1” and “condition_2” values specify the conditions that need to be checked or verified.

Truth Table

For a better understanding, check out the below-given table:

Condition_1 Condition_2 And Operation
T T T
T F F
F T F
F F F

According to the above-given table, if one of the conditions is “False”, the output becomes False.

Example 1: Utilizing “and” Operator to Select Multiple Rows of Pandas DataFrame

The following code is utilized to select multiple rows using the “&” operator:

import pandas
dataframe = pandas.DataFrame({'Name': ['Joseph', 'Lily', 'Anna', 'Henry'],'Age': [21, 29, 20, 28],'Height': [5.8, 5.2, 4.9, 3.7]})
print(dataframe, '\n')
df1 = dataframe[(dataframe['Age'] >= 21) & (dataframe['Height'] >= 5.1)]
print(df1)

Here in this example:

  • The “pandas” module is called/imported, and the DataFrame is created.
  • After that, the multiple conditions are combined with the “&” operator to check whether they are satisfied or not.
  • The desired rows are selected based on the specified conditions.

Output

The specified rows have been selected successfully.

Example 2: Using “and” Operator With Multiple Conditions to Select Multiple Rows of Pandas DataFrame

The below code utilizes the and “&” operator with multiple conditions to select multiple rows of DataFrame:

import pandas
dataframe = pandas.DataFrame({'Name': ['Joseph', 'Lily', 'Anna', 'Henry'],'Age': [21, 29, 20, 28],'Height': [5.8, 5.2, 4.9, 3.7], 'Salary': [452, 1552, 1672, 231]})
print(dataframe, '\n')
df1 = dataframe.loc[(dataframe['Age'] >= 21) & (dataframe['Height'] >= 5.1) & (dataframe['Salary'] > 1000)]
print(df1)

In the above code:

  • The “pandas” module is called/imported, and the DataFrame is constructed with multiple columns.
  • The “dataframe.loc()” method is used along with multiple and “&” operators to check whether the conditions specified become True or not.
  • If the conditions become True, the desired rows have been selected.

Output

The rows based on more than two conditions have been selected.

Bonus Tip: How to Use “or” Operator to Select Multiple Rows of Pandas DataFrame?

Similar to the and “&” operator, the or “|” operator can also be used to select or filter multiple rows of Pandas DataFrame. The or “|” retrieves the “True” if either of the conditions retrieves True. Let’s understand it via the following/below example:

import pandas
dataframe = pandas.DataFrame({'Name': ['Joseph', 'Lily', 'Anna', 'Henry'],'Age': [21, 26, 18, 38],'Height': [5.8, 5.2, 4.9, 3.7], 'Salary': [452, 1552, 1672, 231]})
print(dataframe, '\n')
df1 = dataframe.loc[(dataframe['Age'] >= 26) | (dataframe['Height'] >= 5.1)]
print(df1)

In this particular Python example, the “pandas” module is called, and the DataFrame is created/constructed with multiple columns. Next, the or “|” operator is used to verify the multiple conditions and select rows according to the return value of the condition.

Output

Conclusion

In Python, the and “&” operator is used to combine multiple conditions and filter or select single or multiple DataFrame rows. This operator retrieves the “True” value only when both conditions are satisfied otherwise, it becomes False. This write-up explained a detailed overview of the “And” condition using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.