You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: think_and_compute/lab-09.md
+58-17Lines changed: 58 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,8 @@ By the end of this lab, you will be able to:
18
18
- Load CSV files into pandas DataFrames with correct data types
19
19
- Query and filter DataFrames using `query()` and `iterrows()`
20
20
- Join multiple DataFrames using `merge()`
21
+
- Compute derived values from existing columns
22
+
- Build a new DataFrame from aggregated results
21
23
- Save results to CSV files using `to_csv()`
22
24
```
23
25
@@ -27,7 +29,7 @@ This lab puts into practice the concepts introduced in the [Introduction to Pand
27
29
28
30
## The dataset: Caravaggio's artworks in Italy
29
31
30
-
The dataset for this lab consists of three CSV files stored in the `notebook/` directory:
32
+
The dataset for this lab consists of three CSV files (you can download each file by clicking on its name):
31
33
32
34
-**[`artworks.csv`](notebook/artworks.csv)**: a catalogue of 15 paintings by Caravaggio with their title, year, genre, dimensions (height and width in cm), and the museum where they are held
33
35
-**[`museums.csv`](notebook/museums.csv)**: a list of 11 Italian museums and churches with their city, type, and founding year
@@ -43,7 +45,7 @@ In this first part, you will load the artworks dataset and explore its content u
43
45
44
46
### Exercise 1.1: Load the artworks catalogue
45
47
46
-
Load the file `notebook/artworks.csv` into a pandas DataFrame using `read_csv()`. Make sure to specify `keep_default_na=False` and provide a `dtype` dictionary so that each column is read with the correct data type: `"string"` for text columns and `"int"` for `id`, `year`, `height_cm`, `width_cm`, and `museum_id`. Display the resulting DataFrame.
48
+
Load the file `artworks.csv` into a pandas DataFrame using `read_csv()`. Make sure to specify `keep_default_na=False` and provide a `dtype` dictionary so that each column is read with the correct data type: `"string"` for text columns and `"int"` for `id`, `year`, `height_cm`, `width_cm`, and `museum_id`. Display the resulting DataFrame.
47
49
48
50
```{code-cell} python
49
51
:tags: [hide-cell]
@@ -111,7 +113,7 @@ In real datasets, information is often distributed across multiple tables. In th
111
113
112
114
### Exercise 3.1: Load and join artworks with collections
113
115
114
-
Load `notebook/museums.csv` and `notebook/collections.csv` into two DataFrames, specifying `keep_default_na=False` and appropriate `dtype` dictionaries. Then, use `merge()` to join the artworks DataFrame with the collections DataFrame. The join should match the `id` column in artworks with the `artwork_id` column in collections. Display the resulting DataFrame and observe which columns it contains.
116
+
Load `museums.csv` and `collections.csv` into two DataFrames, specifying `keep_default_na=False` and appropriate `dtype` dictionaries. Then, use `merge()` to join the artworks DataFrame with the collections DataFrame. The join should match the `id` column in artworks with the `artwork_id` column in collections. Display the resulting DataFrame and observe which columns it contains.
115
117
116
118
```{code-cell} python
117
119
:tags: [hide-cell]
@@ -141,7 +143,7 @@ df_art_collections
141
143
142
144
### Exercise 3.2: Chain a second merge and save results
143
145
144
-
Starting from the result of the previous exercise, perform a second `merge()` to add the museum names. Join on the `museum_id` column (present in both the artworks data and the museums data). Then, find all artworks in "excellent" condition and print their title and museum name. Finally, save the resulting DataFrame of excellent-condition artworks to a new CSV file called `notebook/excellent_artworks.csv`, using `to_csv()` with `index=False` to avoid writing the row index.
146
+
Starting from the result of the previous exercise, perform a second `merge()` to add the museum names. Join on the `museum_id` column (present in both the artworks data and the museums data). Then, find all artworks in "excellent" condition and print their title and museum name. Finally, save the resulting DataFrame of excellent-condition artworks to a new CSV file called `excellent_artworks.csv`, using `to_csv()` with `index=False` to avoid writing the row index.
Using all three CSV files, produce a dictionary where each key is a museum name and each value is the number of artworks that museum holds. Print the result.
164
+
Using all three CSV files, produce a summary report as a new DataFrame with one row per museum and the following columns:
165
+
166
+
-`museum`: the museum name
167
+
-`city`: the city where the museum is located
168
+
-`num_artworks`: the number of Caravaggio artworks held by the museum
169
+
-`largest_artwork`: the title of the largest artwork (by area, computed as `height_cm * width_cm`)
170
+
-`excellent_pct`: the percentage of artworks in "excellent" condition (as an integer between 0 and 100)
171
+
172
+
Save the resulting DataFrame to a CSV file called `museum_report.csv` (without the row index) and display it.
163
173
164
174
````{admonition} Solution
165
175
:class: tip, dropdown
166
176
```python
167
-
from pandas import read_csv, merge
177
+
from pandas import read_csv, merge, DataFrame, Series
0 commit comments