0% found this document useful (0 votes)
6 views2 pages

Notes 43 Boolean Indexing

mnbnj

Uploaded by

sadafanjum042004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Notes 43 Boolean Indexing

mnbnj

Uploaded by

sadafanjum042004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Boolean Indexing

Boolean indexing means, the indexes of a dataframe have the boolean values i.e.
True(1) or False(0).

Why Boolean Indexing is required?

We need boolean indexing to divide our dataframe in two groups i.e. True Rows and
False Rows.

While creating a dataframe with Boolean indexes True and False, make sure that both
True and False are not enclosed in quotes (i.e. ‘True’ or ‘False’), otherwise it may give
you Keyerror while accessing data with Boolean indexes using .loc, because ‘True’
and ‘False’ are string values, not the Boolean values.

Example1:

import pandas as pd
dict1={'Name':['Amit', 'Sumit', 'Arpit'],
'Marks':[79, 65,89],
'sport':['Cricket', 'Badminton', 'Tennis']}
df=pd.DataFrame(dict1, index=[True,False,True])
print(df)

Example2:

import pandas as pd
dict1={'Name':['Amit', 'Sumit', 'Arpit'],
'Marks':[79, 65,89],
'sport':['Cricket', 'Badminton', 'Tennis']}
df=pd.DataFrame(dict1, index=[True,False,True])
print(df.loc[True])

Example 3:

import pandas as pd
dict1={'Name':['Amit', 'Sumit', 'Arpit'],
'Marks':[79, 65,89],
'sport':['Cricket', 'Badminton', 'Tennis']}
df=pd.DataFrame(dict1, index=['True','False','True'])
print(df.loc[True])
Example 4:

import pandas as pd
dict1={'Name':['Amit', 'Sumit', 'Arpit'],
'Marks':[79, 65,89],
'sport':['Cricket', 'Badminton', 'Tennis']}
df=pd.DataFrame(dict1, index=[0,1,1])
print(df.loc[1])

Example 5:

import pandas as pd
dict1={'Name':['Amit', 'Sumit', 'Arpit'],
'Marks':[79, 65,89],
'sport':['Cricket', 'Badminton', 'Tennis']}
df=pd.DataFrame(dict1, index=[0,1,1])
print(df.loc[0])

You might also like