Peaks (2 points)¶
Data concerning the world highest mountains are stored in file
peaks.csv. Download it and examine its contents.
We have already read this file obtaining the corresponding
DataFrame, dfpeaks. The following
example shows the names of all the columns of it and a subdataframe
example:
>>> list(dfpeaks.columns)
['Mountain', 'Height', 'Country', 'Prominence', 'Range', 'Parent mountain', 'First ascent', 'Ascents', 'Failed attempts']
>>> dfpeaks[['Mountain', 'Height', 'Country', 'First ascent', 'Ascents']][3:8]
Mountain Height Country First ascent Ascents
3 Lhotse 8516 Nepal + China 1956 26
4 Makalu 8485 Nepal + China 1955 45
5 Cho Oyu 8188 Nepal + China 1954 79
6 Dhaulagiri I 8167 Nepal 1960 51
7 Manaslu 8163 Nepal 1956 49
Save the following functions into file peaks.py and design it by
using the pandas library.
Write function
max_ascents()that takes a DataFrame,dfpeaks, as that described, the name of a country (str) and a year (int). This function considers only the subset of mountains that belong to the given country and such that its first ascent has been in the given year or in a subsequent year, and returns atuplewith two items: the name of the mountain with the maximum number of ascents (str) and this maximum number of ascents (int) (1 point). Examples:>>> max_ascents(dfpeaks, 'Nepal + China', 1955) ('Makalu', 45) >>> max_ascents(dfpeaks, 'Nepal + China', 1956) ('Lhotse', 26) >>> max_ascents(dfpeaks, 'India', 1955) ('Saser Kangri I / K22', 6)
Note
You have more tests in file
test-max_ascents.txtHint
Use indexing, selection and methods
maxandidxmax.Write function
mount_range()that takes a DataFrame,dfpeaks, as that described, and returns another DataFrame resulting from grouping by mountain range (column'Range'). The index of this DataFrame is all the mountain ranges in the given DataFrame and it has three columns with, respectively, the maximum mountain height and the total number of ascents and failed attempts for each mountain range (1 point). Example:>>> d1 = mount_range(dfpeaks) >>> type(d1) <class 'pandas.core.frame.DataFrame'> >>> d1.head() Height Ascents Failed attempts Range Annapurna Himalaya 8091 70 112 Assam Himalaya 7782 2 2 Baltoro Karakoram 8611 181 108 Batura Karakoram 7795 10 12 Daxue Shan 7556 6 7
Note
You have more tests in file
test-range.txtHint
Use methods
groupbyandagg.
Note
To run these functions you don’t actually need to read the given data file. But you need to download and save it into your working folder because the given testfiles read from it to create the DataFrame.