Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Boolean Indexing in Python – A Quick Guide

  • Adrita DasAdrita Das
  • February 27, 2022
  • Python Programming Examples
Boolean Indexing in Python – A Quick Guide

Isn’t it interesting that Boolean values can be used as indexes in dataframes? Boolean indexes represent each row in a DataFrame. Boolean indexing can help us filter unnecessary data from a dataset. Filtering the data can get you some in-depth information that otherwise could not have been found. In this article, we will learn how to use Boolean indexing to filter and segment data. So let’s begin!

Boolean Indexing in Python

Let’s start by creating a DataFrame. We will create a DataFrame using data on the age of a group of candidates taking part in a competition.

import pandas as pd
# Creating a dictionary
data = {'Name':["Tommy","Linda","Justin","Brendon"], 'Age':[31,24,16,22]}
df = pd.DataFrame(data,index=[True,False,True,False])
print(df)

Output

        Name         Age
True     Tommy   31
False    Linda   24
True    Justin   16
False  Brendon   22

1. Using the.loc [] function

This is an excellent and simple function that can help you filter your data according to the Boolean index. Using this function, we can filter out the data with a particular Boolean value. Let’s suppose we pass True to the .loc [] function, we will only get the filtered data having index values as True. We cannot use integers as Boolean values in this method.

For example:

import pandas as pd
# Creating a dictionary
data = {'Name':["Tommy","Linda","Justin","Brendon"], 'Age':[31,24,16,22]}
df = pd.DataFrame(data,index=[True,False,True,False])
print(df.loc[True])

Output:

        Name       Age
True   Tommy   31
True  Justin   16

2. Using .iloc[] function

iloc[] function only accepts integer values and so we need to pass integer values to the function.

For example:

import pandas as pd
# Creating a dictionary
data = {'Name':["Tommy","Linda","Justin","Brendon"], 'Age':[31,24,16,22]}
df = pd.DataFrame(data,index=[1,0,0,1])
print(df.iloc[1])

Output:

Name    Linda
Age        24
Name: 0, dtype: object

3. Using the .ix[] function

This is also a similar kind of method to the above one, but we can use integers as Boolean values in this case. So, for instance, if we assign the index values as 1 and 0, we can filter the rows having index values of 0 or 1.

import pandas as pd
# Creating a dictionary
data = {'Name':["Tommy","Linda","Justin","Brendon"], 'Age':[31,24,16,22]}
df = pd.DataFrame(data,index=[1,1,0,0])
print(df.ix[0])

Output:

           Name       Age
0       Justin          16
0       Brendon     22

Conclusion

In summary, we learned how to use boolean indexing in python and filter the useful data. Hope you found this article helpful.

Adrita Das
Adrita Das
Articles: 24
How to Iterate Over Rows in Pandas Dataframe?
Previous Post How to Iterate Over Rows in Pandas Dataframe?
Next Post Are Arrays Mutable in Python?
Are Arrays Mutable in Python?

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.