
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Boolean Indexing in Pandas
Boolean indexing helps us to select the data from the DataFrames using a boolean vector. We need a DataFrame with a boolean index to use the boolean indexing. Let's see how to achieve the boolean indexing.
- Create a dictionary of data.
- Convert it into a DataFrame object with a boolean index as a vector.
- Now, access the data using boolean indexing.
See the example below to get an idea.
Example
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)
Output
If you run the above program, you will get the following results.
Name Age True Hafeez 19 False Srikanth 20 True Rakesh 19
Now, we can access the DataFrame by passing booleans to the methods loc[], iloc[], ix[]. Let's see them all.
Example
# accessing using .loc() print(data_frame.loc[True])
Output
If run the above code, you will get the following results.
Name Age True Hafeez 19 True Rakesh 19
Example
# accessing using .iloc() print(data_frame.iloc[1]) # iloc methods takes only integers so, we are passing 1 i nsted of True. Both are same.
Output
If run the above code, you will get the following results.
Name Srikanth Age 20 dtype: object
Example
# accessing using .ix[] # we can pass both boolean or integer values to .ix[] print(data_frame.ix[True]) print() print(data_frame.ix[1])
Output
If run the above code, you will get the following results.
Name Age True Hafeez 19 True Rakesh 19 Name Srikanth Age 20 dtype: object
Another way to use the boolean index is to directly pass the boolean vector to the DataFrame. It will print all the rows with the value True. Let's see one example.
Example
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) print(data_frame)
Output
If run the above code, you will get the following results.
Name Age 0 Hafeez 19 1 Srikanth 20 2 Rakesh 19
Now, we can pass the boolean vector to the DataFrame to access the data.
Example
# passing boolean vector to data_frame index print(data_frame[[True, True, False]])
Output
If run the above code, you will get the following results. We got the row only that is True.
Name Age 0 Hafeez 19 1 Srikanth 20
Conclusion
If you have any doubts about the Boolean index, let me know in the comment section.
- Related Articles
- Boolean Indexing in Python
- Python Pandas - Create a Subset DataFrame using Indexing Operator
- How to access Pandas Series elements by using indexing?
- Bitmap Indexing in DBMS
- Indexing numbers to alphabets in JavaScript
- Python Indexing a sublist
- Explain the concept of indexing in DBMS
- Difference between indexing and slicing in Python
- What is a Negative Indexing in Python?
- Basic Slicing and Advanced Indexing in NumPy Python
- Removing Array Element and Re-Indexing in PHP
- Create a Boolean object from Boolean value in Java
- What are the techniques of Text Indexing?
- Convert Java Boolean Primitive to Boolean object
- Explain non-boolean value coercion to a boolean one in JavaScript?
