Block 23: Selecting Rows & Columns
Access subsets of a DataFrame with loc, iloc, and column selection.
Concepts
- df['col'] and df[['col1','col2']] for columns
- df.loc[row_label, col_label] — label-based
- df.iloc[row_num, col_num] — position-based
- Boolean indexing: df[df['col'] > value]
Code Examples
See exercise below.
Exercise
Select only numeric columns from the Iris dataset. Use loc to get rows where species == 'setosa' and only the petal columns.
Homework
What is the key difference between loc and iloc? When would each be confusing?