Exercise

Slicing and indexing trees

Imagine you are a researcher working with data from New York City's tree census. Each row of the tree_census 2D array lists information for a different tree: the tree ID, block ID, trunk diameter, and stump diameter in that order. Living trees do not have stump diameters, which explains why there are so many zeros in that column. Column order is important because NumPy does not have column names! The first and last three rows of tree_census are shown below.

array([[     3, 501451,     24,      0],
       [     4, 501451,     20,      0],
       [     7, 501911,      3,      0],
       ...,
       [  1198, 227387,     11,      0],
       [  1199, 227387,     11,      0],
       [  1210, 227386,      6,      0]])

In this exercise, you'll be working specifically with the second column, representing block IDs: your research requires you to select specific city blocks for further analysis using NumPy slicing and indexing. numpy is loaded as np, and the tree_census 2D array is available.

Instructions 1/3

undefined XP
  • 1
    • Select all rows of data from the second column, representing block IDs; save the resulting array as block_ids.
    • Print the first five block IDs from block_ids.
  • 2
    • Select the tenth block ID from block_ids, saving the result as tenth_block_id.
  • 3
    • Select five consecutive block IDs from block_ids, starting with the tenth ID, and save as block_id_slice