You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _episodes/03-index-slice-subset.md
+12-2Lines changed: 12 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -358,8 +358,18 @@ gives the **output**
358
358
Remember that Python indexing begins at 0. So, the index location [2, 6]
359
359
selects the element that is 3 rows down and 7 columns over in the DataFrame.
360
360
361
-
It is worth noting that rows are selected when using `loc` with a single list of labels (or `iloc` with a single list of integers). However, unlike `loc` or `iloc`, indexing a data frame directly with labels will select columns, while ranges of integers will select rows. Direct indexing of rows is redundant with using `iloc`, and will raise a `KeyError` if a single integer or list is used; the error will also occur if index labels are used without `loc` (or column labels used with it).
362
-
A useful rule of thumb is the following: integer-based slicing is best done with `iloc` and will avoid errors (and is generally consistent with indexing of Numpy arrays), label-based slicing of rows is done with `loc`, and slicing of columns by directly indexing column names.
361
+
It is worth noting that rows are selected when using `loc` with a single list of
362
+
labels (or `iloc` with a single list of integers). However, unlike `loc` or `iloc`,
363
+
indexing a data frame directly with labels will select columns (e.g.
364
+
`surveys_df['species_id', 'plot_id', 'weight']`), while ranges of integers will
365
+
select rows (e.g. surveys_df[0:13]). Direct indexing of rows is redundant with
366
+
using `iloc`, and will raise a `KeyError` if a single integer or list is used; the
367
+
error will also occur if index labels are used without `loc` (or column labels used
368
+
with it).
369
+
A useful rule of thumb is the following: integer-based slicing is best done with
370
+
`iloc` and will avoid errors (and is generally consistent with indexing of Numpy
371
+
arrays), label-based slicing of rows is done with `loc`, and slicing of columns by
0 commit comments