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
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,7 +118,7 @@ the related Python data type dictionary).
118
118
> the names of built-in data structures and methods. For example, a _list_ is a built-in
119
119
> data type. It is possible to use the word 'list' as an identifier for a new object,
120
120
> for example `list = ['apples', 'oranges', 'bananas']`. However, you would then
121
-
> be unable to create an empty list using `list()` or convert a tuple to a
121
+
> be unable to create an empty list using `list()` or convert a tuple to a
122
122
> list using `list(sometuple)`.
123
123
{: .callout}
124
124
@@ -358,22 +358,28 @@ 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.
0 commit comments