Comparing row filtering and index filtering
It is possible to replicate specific cases of Boolean selection by taking advantage of the index.
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 performances against one another.
Personally, I prefer to filter by columns (using Boolean arrays) rather than on the index. Column filtering is more powerful as you can use other logical operators and filter on multiple columns.
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() INSTNM ... GRAD_/_SUPP 3610 Abilene Christian University ... 25985 3611 Alvin Community College...