How to do it...

  1. 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

  1. 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()
  1. Let's compare the speed of both methods:
>>> %timeit college[college['STABBR'] ...

Get Numerical Computing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.