Agroalimentary (2 points)¶
From the Catalan Government web we have downloaded the file
agroalimentary.csv related to Catalan agroalimentary
companies, that you already know. We have extended it with a randomly
generated column named Facturacio showing the company billing in
thousands of euros (int). Download and study this file. We
have already created a DataFrame with
this data.
The following example shows the names of all the columns of this
DataFrame, then a subdataframe example
with columns corresponding to the company name (Establiment), the
region (Comarca), the company type (Titularitat) and the
billing (Facturacio), and, below, the same subdataframe with
colums Establiment and Sector:
>>> list(dfagro.columns)
['Establiment', 'Adreça', 'Codi postal', 'Municipi', 'Comarca', 'Titularitat', 'Sector', 'Latitud', 'Longitud', 'Facturacio']
>>> dfagro[['Establiment', 'Comarca', 'Titularitat', 'Facturacio']][15:25]
Establiment Comarca Titularitat Facturacio
15 AVICOLA SURIA, SA Bages SOCIETAT ANÒNIMA 1396
16 BALAGUER I CABRÉ, SL Priorat SOCIETAT LIMITADA 5496
17 BIO OLEICS BELIANES, SL Urgell SOCIETAT LIMITADA 4194
18 BODEGAS J. TRIAS, SA Alt Penedès SOCIETAT ANÒNIMA 3183
19 BODEGAS VILA CORONA SL Pallars Jussà SOCIETAT LIMITADA 644
20 BODEGUES VISENDRA, SCP Alt Camp ALTRES 704
21 CA L'ARENYS, SL Bages SOCIETAT LIMITADA 4115
22 CAL GASSET, SL Conca de Barberà SOCIETAT LIMITADA 6187
23 CANAL SELMA, JOAQUIM Ripollès PARTICULAR 5710
24 CARNE GUAL, ESTEVE Vallès Occidental PARTICULAR 1964
>>> dfagro[['Establiment', 'Sector']][15:25]
Establiment Sector
15 AVICOLA SURIA, SA 10.8 - Fabricació d'altres productes alimentaris
16 BALAGUER I CABRÉ, SL 11.0 - Fabricació de begudes
17 BIO OLEICS BELIANES, SL 10.4 - Fabricació d'olis i greixos vegetals i ...
18 BODEGAS J. TRIAS, SA 11.0 - Fabricació de begudes
19 BODEGAS VILA CORONA SL 11.0 - Fabricació de begudes
20 BODEGUES VISENDRA, SCP 11.0 - Fabricació de begudes
21 CA L'ARENYS, SL 11.0 - Fabricació de begudes
22 CAL GASSET, SL 11.0 - Fabricació de begudes
23 CANAL SELMA, JOAQUIM 10.5 - Fabricació de productes lactis
24 CARNE GUAL, ESTEVE 16. - Indústries forestals
Save the two following functions to file agroalim.py.
Write function
agroalim1()that takes a DataFrame,dfagro, as that described and a string (str) corresponding to a region,'Comarca', and returns a dictionary (dict) in which keys are company types,'Titularitat'(str), and values are the number of companies of this type, corresponding to the given region (1 point). Examples:>>> d1 = agroalim1(dfagro, 'Conca de Barberà') >>> if d1 !={'COOPERATIVA': 2, 'PARTICULAR': 3, 'SOCIETAT LIMITADA': 10}: ... print(d1) >>> d2 = agroalim1(dfagro, 'Barcelonès') >>> if d2 !={'PARTICULAR': 6, 'SOCIETAT ANÒNIMA': 15, 'SOCIETAT LIMITADA': 19}: ... print(d2)
Note
You have more tests in file
test-agroalim1.txtHint
Use selection, methods
groupbyandsizeand functiondict.Write function
agroalim2()that takes a DataFrame,dfagro, as that described, and two values (int) corresponding to two billings, \(b1\) and \(b2\), such that \(b1<b2\). This function considers the subset of companies with a billing \(b\) such that \(b1 \leq b \leq b2\) and returns a tuple with two values: the name of the sector ,'Sector', (str) for which the billing sum of the companies of this subset corresponding to this sector is minimum, and the corresponding minimum billing sum (int) (1 point). Examples:>>> agroalim2(dfagro, 100, 500) ('10.6 - Fabricació de productes de molineria, midons i productes amilacis', 206) >>> agroalim2(dfagro, 5000, 9000) ('12.0 - Indústries del tabac', 5665)
Note
You have more tests in file
test-agroalim2.txtHint
Use selection and methods
groupby,sum,idxminandmin.
Note
To run your 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.