Agroalimentary (1.5 points)

From the Catalan government web we have downloaded the file agroalimentary.csv related to Catalan agroalimentary companies. Download and study this file. We have already created a DataFrame with this data.

The following example shows the names of the columns of this DataFrame and a subdataframe example with columns corresponding to the establishment name (Establiment), the region (Comarca), the company type (Titularitat) and the latitude (Latitud):

>>> list(dfagro.columns)
['Establiment', 'Adreça', 'Codi postal', 'Municipi', 'Comarca', 'Titularitat', 'Sector', 'Latitud', 'Longitud']

>>> dfagro[['Establiment', 'Comarca', 'Titularitat', 'Latitud']][15:25]
                Establiment            Comarca        Titularitat   Latitud
15        AVICOLA SURIA, SA              Bages   SOCIETAT ANÒNIMA  41.83801
16     BALAGUER I CABRÉ, SL            Priorat  SOCIETAT LIMITADA  41.19284
17  BIO OLEICS BELIANES, SL             Urgell  SOCIETAT LIMITADA  41.56090
18     BODEGAS J. TRIAS, SA        Alt Penedès   SOCIETAT ANÒNIMA  41.34606
19   BODEGAS VILA CORONA SL      Pallars Jussà  SOCIETAT LIMITADA  42.15617
20   BODEGUES VISENDRA, SCP           Alt Camp             ALTRES  41.35949
21          CA L'ARENYS, SL              Bages  SOCIETAT LIMITADA  41.84706
22           CAL GASSET, SL   Conca de Barberà  SOCIETAT LIMITADA  41.45733
23     CANAL SELMA, JOAQUIM           Ripollès         PARTICULAR  42.22751
24       CARNE GUAL, ESTEVE  Vallès Occidental         PARTICULAR  41.61314

Save the following functions to file p4.py.

  1. Write function agro1() that takes a DataFrame, df, as that described and a string corresponding to a company type, and returns the percentage of companies of this type. (0.75 points)

    Examples:

    >>> round(agro1(dfagro[15:25], 'SOCIETAT LIMITADA'), 2)
    50.0
    >>> round(agro1(dfagro[15:25], 'PARTICULAR'), 2)
    20.0
    

    Note

    You have more tests in file test-agroalimentary-1.txt

Hint

Use selection.

  1. Write function agro2() that takes a DataFrame, df, as that described, and a string with the name of a region. This function computes the most northern company (i.e. the one with the maximum latitude) in the given region, and returns a tuple with two elements: the establishment name and latitude of this company. (0.75 points)

    Examples:

    >>> agro2(dfagro[15:25], 'Bages')
    ("CA L'ARENYS, SL", 41.84706)
    
    >>> agro2(dfagro[:20], 'Alt Penedès')
    ('AGRICOLA HORTONS, SL', 41.47967)
    

    Note

    You have more tests in file test-agroalimentary-2.txt

Hint

Use selection, indexing and method idxmax().

Note

To run your functions you don’t actually need to read the given data file. But you need to download it because the given testfiles read from it to create the DataFrame.