Replicating boolean indexing with index selection
It is possible to replicate specific cases of boolean selection by taking advantage of the index. Selection through the index is more intuitive and makes for greater readability.
Getting ready
In this recipe, we use the college
dataset to select all institutions from a particular state with both boolean indexing and index selection and then compare each of their performance against one another.
How to do it...
- Read in the
college
dataset and use boolean indexing to select all institutions from the state of Texas (TX):
>>> college = pd.read_csv('data/college.csv') >>> college[college['STABBR'] == 'TX'].head()
Pandas official documentation on
- To replicate this using index selection, we need to move the
STABBR
column into the index. We can then use label-based selection with the.loc
indexer:
>>> college2 = college.set_index('STABBR') >>> college2.loc['TX'].head()
- Let's compare the speed of both methods:
>>>...