Selecting DataFrame rows
The most explicit and preferred way to select DataFrame rows is with the .iloc
and .loc
indexers. They are capable of selecting rows or columns independently and simultaneously.
Getting ready
This recipe shows you how to select rows from a DataFrame using the .iloc
and .loc
indexers.
Â
How to do it...
- 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...