Mountains (2 points)

Data concerning the world highest mountains are stored in file peaks.csv. Download it and examine its content.

We have already read this file obtaining the corresponding DataFrame, dfp. The following example shows the names of all the columns of it and a subdataframe example:

>>> dfp = pd.read_csv('solution/peaks.csv')
>>> list(dfp.columns)
['Mountain', 'Height', 'Country', 'Prominence', 'Range', 'Parent mountain', 'First ascent', 'Ascents', 'Failed attempts']

>>> dfp[['Mountain', 'Height', 'Range', 'Parent mountain', 'Ascents', 'Failed attempts']][3:8]
       Mountain  Height                Range Parent mountain  Ascents  Failed attempts
3        Lhotse    8516  Mahalangur Himalaya   Mount Everest       26               26
4        Makalu    8485  Mahalangur Himalaya   Mount Everest       45               52
5       Cho Oyu    8188  Mahalangur Himalaya   Mount Everest       79               28
6  Dhaulagiri I    8167  Dhaulagiri Himalaya              K2       51               39
7       Manaslu    8163     Manaslu Himalaya         Cho Oyu       49               45

Save the following functions into file mountains.py

  1. (1 point) Write function failed() that takes a DataFrame, dfp, as that described, the name of a mountain range (str) and two mountain heights (two int), h1 and h2 ( h1 < h2). This function considers only the subdataframe with those mountains that belong to the given range and such that its height is between h1 and h2 (both included), and returns a tuple with the maximum and the minimum number (two int) of failed attempts of this subdataframe of mountains. Examples:

    >>> failed(dfp, 'Mahalangur Himalaya', 8000, 9000)
    (121, 26)
    >>> failed(dfp, 'Baltoro Karakoram', 8000, 9000)
    (44, 12)
    

    Note

    You have more tests in file test-failed.txt

    Hint

    Use selection and methods max and min.

  2. (1 point) Write function parent() that takes a DataFrame, dfp, as that described, and returns a Series with the total number of ascents for each parent mountain, sorted in descending order. Examples:

    >>> s1 = parent(dfp)
    >>> type(s1)
    <class 'pandas.core.series.Series'>
    >>> len(s1)
    47
    >>> s1.head()
    Parent mountain
    Mount Everest    250
    Cho Oyu          136
    Gasherbrum I     118
    K2                88
    Dhaulagiri        76
    Name: Ascents, dtype: int64
    
    >>> s1['Batura Sar']
    24
    

    Note

    You have more tests in file test-parent.txt

    Hint

    Use methods groupby, sum and sort_values.

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.