Selecting DataFrame rows
The most explicit and preferred way to select DataFrame rows is with .iloc
and .loc
. They are both capable of selecting by rows or by rows and columns.
This recipe shows you how to select rows from a DataFrame using the .iloc
and .loc
indexers:
- Read in the college dataset, and set the index as the institution name:
>>> college = pd.read_csv( ... "data/college.csv", index_col="INSTNM" ... ) >>> college.sample(5, random_state=42) CITY STABBR ... MD_EARN_WNE_P10 GRAD_DEBT_MDN_SUPP INSTNM ... Career Po... San Antonio TX ... 20700 14977 Ner Israe... Baltimore MD ... PrivacyS... PrivacyS... Reflectio... Decatur IL ... NaN PrivacyS... Capital A... Baton Rouge LA ... 26400 PrivacyS... West Virg... Montgomery WV ... 43400 23969 <BLANKLINE>...