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.

Updated on: 02-Jan-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements