1. Learn
  2. /
  3. Courses
  4. /
  5. Data Manipulation with pandas

Exercise

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 and last can be strings.
  • To slice at inner levels, first and last 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.

Instructions

100 XP
  • 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.