How to use Bitwise OR function in Pandas Dataframe in Python
In this tutorial, we will learn how to use bitwise OR function and pop function in Pandas Dataframe in Python. Bitwise OR function can be used where part of data can be selected which follows at least one of the conditions given. ‘OR’ as an operator in itself means that it will return the values which satisfy at least one of the many conditions that is applied to it. For example, I want to buy fruits but can only take one type of fruit from apples, oranges, and peaches. So I define price as a criterion for choosing. Since I have only 50 R.s with me, I can put a condition that ‘apple less than 50’ OR ‘orange less than 50’ OR ‘peach less than 50’. Whichever fruit follows the condition will be chosen. Let us learn how we can do this in Python with the help of a similar example.
Using Bitwise OR function in Pandas Dataframe in Python
Let us make a simple Dataframe consisting of three students ‘Ankit’, ‘Arpit’, and ‘Arun’. To learn how to make a Dataframe please Click here. The student’s name will be a part of the Dataframe index. Consider their marks in two subjects ‘Maths’ and ‘Physics’. Let us assign the name of the Dataframe as ‘df’. Assuming the teacher wants to know how many students have more than 15 marks in at least one of the subjects. This condition is applied by using Bitwise OR function (symbolized by ‘’). We are assigning the name “Choose” to this Dataframe as here we are going to choose students satisfying our condition.
Code for using OR function
import pandas as pd import numpy as np data={'maths':[10,20,10],'physics':[30,10,10]} Df=pd.DataFrame(data,index=["Ankit","Arpit","Arun"]) Df
Output:
Now for applying the condition on Dataframe using Bitwise OR function:
Choose=Df[(Df['maths']>15)(Df['physics']>15)] Choose
Output:
Maths 
Physics 


Ankit 
10 
30 
Arpit 
20 
10 
From this output, we can see that the two students Ankit and Arpit got selected as they met the condition. Ankit has more than 15 marks in Physics whereas Arpit has more than 15 marks in Maths. Arun did not get selected as he did not get more than 15 marks in either of the subjects. From this simple and easy to replicate example one can learn to use the Bitwise OR function to filter data complying to certain conditions.
Leave a Reply