# Boolean Indexing in Pandas

Let's understand Boolean Indexing in Pandas with examples. By Pranit Sharma Last updated : September 21, 2023

## Indexing in Pandas

Index in pandas is just the number of rows defined in a Series or DataFrame. The index always starts from 0 to n-1 where n is the number of rows. The index is used in indexing which is a method of traversing or selecting particular rows and columns or selecting all the rows and columns.

## Boolean Indexing

In programming, we sometimes use some specific values that only have two values, either True or False. These values are known as Boolean values. Boolean Indexing in Pandas is nothing but indexing the rows of the pandas DataFrame with their actual values (True or False) rather than naming them with a string or an integer value.

## Performing Boolean Indexing in Pandas

To achieve Boolean indexing, we simply assign a list of Boolean values to the index values while defining a DataFrame.

Note

To work with pandas, we need to import pandas package first, below is the syntax:

```import pandas as pd
```

Let us understand with the help of an example.

## Create and print the dataframe

```# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
'Name':["Ayushi", "Parth", "Sudhir", "Ganesh"],
'Post': ["HR", "SDE", "Data-Analyst", "SDE"],
'Salary':[40000, 50000, 80000, 52000]
}

# Creating a DataFrame
df = pd.DataFrame(d, index = [True, False, True, False])

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")
```

The output of the above program is: ## Performing Boolean indexing in Pandas

We can also access any of the subsets of this DataFrame with the condition of True or False, if the True value is passed, the subset having values True will be resulted otherwise subset having the value False will have resulted.

```# Accessing a subset having value True

print(df.loc[True])
```

The output of the above program is:
{"ft_lang_label":"__label__en","ft_lang_prob":0.749337,"math_prob":0.84361947,"size":1945,"snap":"2023-40-2023-50","text_gpt3_token_len":458,"char_repetition_ratio":0.15095311,"word_repetition_ratio":0.018867925,"special_character_ratio":0.2344473,"punctuation_ratio":0.12295082,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98893154,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-11T15:24:28Z\",\"WARC-Record-ID\":\"<urn:uuid:1ee03e58-404c-4e82-9165-f6cd4c8064ea>\",\"Content-Length\":\"147988\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a6c1afa9-2b81-4626-bed1-dd7913e5e2be>\",\"WARC-Concurrent-To\":\"<urn:uuid:9e291891-7af0-45c3-a343-5300e82203b0>\",\"WARC-IP-Address\":\"172.67.199.84\",\"WARC-Target-URI\":\"https://www.includehelp.com/python/boolean-indexing-in-pandas.aspx\",\"WARC-Payload-Digest\":\"sha1:PQLBXNFR2WGITWF2BYT5HTFD4V53OWHI\",\"WARC-Block-Digest\":\"sha1:FVR3SHET6DFLUNGELBME2S7AXRBRDVPH\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679515260.97_warc_CC-MAIN-20231211143258-20231211173258-00126.warc.gz\"}"}