Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Boolean Indexing in Pandas
Boolean indexing in Pandas allows you to select data from DataFrames using boolean vectors. This powerful feature lets you filter rows based on True/False values, either through boolean indices or by passing boolean arrays directly to the DataFrame.
Creating a DataFrame with Boolean Index
First, let's create a DataFrame with a boolean index vector ?
import pandas as pd
# data
data = {
'Name': ['Hafeez', 'Srikanth', 'Rakesh'],
'Age': [19, 20, 19]
}
# creating a DataFrame with boolean index vector
data_frame = pd.DataFrame(data, index=[True, False, True])
print(data_frame)
Name Age
True Hafeez 19
False Srikanth 20
True Rakesh 19
Accessing Data Using Boolean Indices
Using .loc[] Method
The loc[] method allows label-based selection using boolean indices ?
import pandas as pd
data = {
'Name': ['Hafeez', 'Srikanth', 'Rakesh'],
'Age': [19, 20, 19]
}
data_frame = pd.DataFrame(data, index=[True, False, True])
# accessing using .loc()
print(data_frame.loc[True])
Name Age
True Hafeez 19
True Rakesh 19
Using .iloc[] Method
The iloc[] method uses integer positions for selection ?
import pandas as pd
data = {
'Name': ['Hafeez', 'Srikanth', 'Rakesh'],
'Age': [19, 20, 19]
}
data_frame = pd.DataFrame(data, index=[True, False, True])
# accessing using .iloc() - takes integer position
print(data_frame.iloc[1]) # Position 1 corresponds to False index
Name Srikanth Age 20 Name: False, dtype: object
Boolean Vector Filtering
You can directly pass a boolean vector to filter DataFrame rows ?
import pandas as pd
# data with regular integer index
data = {
'Name': ['Hafeez', 'Srikanth', 'Rakesh'],
'Age': [19, 20, 19]
}
data_frame = pd.DataFrame(data)
print("Original DataFrame:")
print(data_frame)
print()
# filtering using boolean vector
print("Filtered DataFrame:")
print(data_frame[[True, True, False]])
Original DataFrame:
Name Age
0 Hafeez 19
1 Srikanth 20
2 Rakesh 19
Filtered DataFrame:
Name Age
0 Hafeez 19
1 Srikanth 20
Practical Boolean Indexing with Conditions
Boolean indexing is most useful when combined with conditional expressions ?
import pandas as pd
data = {
'Name': ['Hafeez', 'Srikanth', 'Rakesh', 'Priya'],
'Age': [19, 20, 19, 22],
'Score': [85, 92, 78, 95]
}
df = pd.DataFrame(data)
# Filter students with age greater than 19
print("Students older than 19:")
print(df[df['Age'] > 19])
print()
# Filter students with score above 80
print("Students with score > 80:")
print(df[df['Score'] > 80])
Students older than 19:
Name Age Score
1 Srikanth 20 92
3 Priya 22 95
Students with score > 80:
Name Age Score
0 Hafeez 19 85
1 Srikanth 20 92
3 Priya 22 95
Comparison of Boolean Indexing Methods
| Method | Input Type | Use Case |
|---|---|---|
.loc[boolean] |
Boolean labels | Label-based selection |
.iloc[int] |
Integer positions | Position-based selection |
df[boolean_vector] |
Boolean array | Row filtering |
df[condition] |
Boolean Series | Conditional filtering |
Conclusion
Boolean indexing in Pandas provides flexible data filtering capabilities. Use boolean vectors for simple row selection and conditional expressions for dynamic filtering based on DataFrame values.
