Slicing index values
Slicing lets you select consecutive elements of an object using first:last
syntax. DataFrames can be sliced by index values or by row/column number; we'll start with the first case. This involves slicing inside the .loc[]
method.
Compared to slicing lists, there are a few things to remember.
- You can only slice an index if the index is sorted (using
.sort_index()
). - To slice at the outer level,
first
andlast
can be strings. - To slice at inner levels,
first
andlast
should be tuples. - If you pass a single slice to
.loc[]
, it will slice the rows.
pandas
is loaded as pd
. temperatures_ind
has country and city in the index, and is available.
This exercise is part of the course
Data Manipulation with pandas
Exercise instructions
- Sort the index of
temperatures_ind
. - Use slicing with
.loc[]
to get these subsets:- from Pakistan to Russia.
- from Lahore to Moscow. (This will return nonsense.)
- from Pakistan, Lahore to Russia, Moscow.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Sort the index of temperatures_ind
temperatures_srt = ____
# Subset rows from Pakistan to Russia
print(____)
# Try to subset rows from Lahore to Moscow
print(____)
# Subset rows from Pakistan, Lahore to Russia, Moscow
print(____)