July 2019
Beginner to intermediate
740 pages
16h 52m
English
When we want to extract certain rows (slices) from our dataframe, we use slicing. DataFrame slicing works similarly to slicing with other Python objects, such as lists and tuples, with the first index being inclusive and the last being one exclusive:
>>> df[100:103]
When specifying a slice of 100:103, we get back rows 100, 101, and 102:
| alert | ... | mag | magType | ... | time | title | ... | |
|---|---|---|---|---|---|---|---|---|
| 100 | NaN | ... | 1.20 | ml | ... | 1539435449480 | M 1.2 - 25km NW of Ester, Alaska | ... |
| 101 | NaN | ... | 0.59 | md | ... | 1539435391320 | M 0.6 - 8km ESE of Mammoth Lakes, CA | ... |
| 102 | NaN | ... | 1.33 | ml | ... | 1539435293090 | M 1.3 - 8km ENE of Aguanga, CA | ... |
We can combine our row and column selections in what is known as chaining:
>>> df[['title', 'time']][100:103]
First, ...
Read now
Unlock full access