Slicing rows lazily
The previous recipes in this chapter showed how the .iloc
and .loc
indexers were used to select subsets of both Series and DataFrames in either dimension. A shortcut to select the rows exists with just the indexing operator itself. This is just a shortcut to show additional features of pandas, but the primary function of the indexing operator is actually to select DataFrame columns. If you want to select rows, it is best to use .iloc
or .loc
, as they are unambiguous.
Â
Â
Â
Getting ready
In this recipe, we pass a slice object to both the Series and DataFrame indexing operators.
How to do it...
- Read in the college dataset with the institution name as the index and then select every other row from index 10 to 20:
>>> college = pd.read_csv('data/college.csv', index_col='INSTNM') >>> college[10:20:2]
- This same slicing exists with Series:
>>> city = college['CITY'] >>> city[10:20:2] INSTNM Birmingham Southern College Birmingham Concordia...