Home assignment 2#
Last updated: 2026-02-12 16:07:33
Question 1#
Read the DEM of the Carmel in
'carmel.csv'into anndarrayobject.Calculate how many cells/pixels are in each of 100-m elevation “bins” starting with
-100and ending with600meters, i.e.,-100-0,0-100, …,500-600.The counts need to include the start point but not the end point, i.e., the first bin is \(-100 ≤ x < 0\), the second bin is \(0 ≤ x < 100\), and so on.
Store the results in a dictionary, with the dictionary keys being the bin labels, and dictionary values being the pixel counts.
Print the dictionary.
{'-100 - 0': np.int64(380),
'0 - 100': np.int64(80438),
'100 - 200': np.int64(40877),
'200 - 300': np.int64(20841),
'300 - 400': np.int64(8998),
'400 - 500': np.int64(5116),
'500 - 600': np.int64(476)}
Question 2#
Read the
'world_cities.csv'file into aDataFrameobjectChoose a city that starts with the same English letter as your first name (for example,
'Mexico City'if your first name is Michael). You can do this step manually, e.g., in Excel, and not in your Python code.Subset the cities which are in the same \(1 \times 1\) degrees “cell” of longitude and latitude. For example, if your chosen city is Mexico City which is at
(-99.14, 19.43)(lon, lat), then you need to subset the cities with longitude between-100and-99, and latitude between19and20.Sort the resulting table by population (column
'pop'), from biggest city to smallestPrint the result
| city | country | pop | lat | lon | capital | |
|---|---|---|---|---|---|---|
| 23660 | Mexico City | Mexico | 8659409 | 19.43 | -99.14 | 1 |
| 10135 | Ecatepec | Mexico | 1844447 | 19.60 | -99.05 | 0 |
| 25921 | Nezahualcoyotl | Mexico | 1230816 | 19.41 | -99.03 | 0 |
| 25496 | Naucalpan | Mexico | 847600 | 19.48 | -99.23 | 0 |
| 38320 | Tlalnepantla | Mexico | 715026 | 19.54 | -99.19 | 0 |
| ... | ... | ... | ... | ... | ... | ... |
| 8506 | Cuautlalpan | Mexico | 8349 | 19.69 | -99.29 | 0 |
| 33002 | San Juan Tilapa | Mexico | 8188 | 19.22 | -99.66 | 0 |
| 38672 | Totocuitlapilco | Mexico | 8134 | 19.23 | -99.58 | 0 |
| 16288 | Jilotzingo | Mexico | 8050 | 19.87 | -99.07 | 0 |
| 7383 | Chiconautla | Mexico | 8010 | 19.62 | -99.01 | 0 |
77 rows × 6 columns
Hint: you can use the
numpymethods.floorand.ceilto get the nearest whole number below and above the given value, respectively.Note: do not use any longitude or latitude values in your code! The only specific value in the code should be the city name you chose, all other values need to be calculated.