- Read in the college dataset, and set the index as the institution name:
>>> college = pd.read_csv('data/college.csv', index_col='INSTNM')
>>> college.head()
- Pass an integer to the .iloc indexer to select an entire row at that position:
>>> college.iloc[60]
CITY Anchorage
STABBR AK
HBCU 0
...
UG25ABV 0.4386
MD_EARN_WNE_P10 42500
GRAD_DEBT_MDN_SUPP 19449.5
Name: University of Alaska Anchorage, Length: 26, dtype: object
- To get the same row as the preceding step, pass the index label to the .loc indexer:
>>> college.loc['University of Alaska Anchorage']
CITY Anchorage
STABBR AK
HBCU 0
...
UG25ABV 0.4386
MD_EARN_WNE_P10 42500
GRAD_DEBT_MDN_SUPP 19449.5
Name: University of Alaska Anchorage, Length: 26, dtype: object
- To select a disjointed set of rows as a DataFrame, pass a list of integers to the .iloc indexer:
>>> college.iloc[[60, 99, 3]]
- The same DataFrame from step 4 may be reproduced using .loc by passing it a list of the exact institution names:
>>> labels = ['University of Alaska Anchorage',
'International Academy of Hair Design',
'University of Alabama in Huntsville']
>>> college.loc[labels]
- Use slice notation with .iloc to select an entire segment of the data:
>>> college.iloc[99:102]
- Slice notation also works with the .loc indexer and is inclusive of the last label:
>>> start = 'International Academy of Hair Design'
>>> stop = 'Mesa Community College'
>>> college.loc[start:stop]