From e6fcfd8789e66aa3cc9dadb5f970be270ee4de3c Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Tue, 5 Mar 2024 00:50:41 +0000 Subject: [PATCH] differences for PR #674 --- 14-looping-data-sets.md | 20 ++++++++++++++++---- md5sum.txt | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/14-looping-data-sets.md b/14-looping-data-sets.md index ec19be367..6e95b24c3 100644 --- a/14-looping-data-sets.md +++ b/14-looping-data-sets.md @@ -180,7 +180,10 @@ What other special strings does the [`float` function][float-function] recognize Write a program that reads in the regional data sets and plots the average GDP per capita for each region over time -in a single chart. +in a single chart. Pandas will raise an error if it encounters +non-numeric columns in a dataframe computation so you may need +to either filter out those columns or tell pandas to ignore them. + ::::::::::::::: solution @@ -200,8 +203,17 @@ for filename in glob.glob('data/gapminder_gdp*.csv'): # we will split the string using the split method and `_` as our separator, # retrieve the last string in the list that split returns (`.csv`), # and then remove the `.csv` extension from that string. + # NOTE: the pathlib module covered in the next callout also offers + # convenient abstractions for working with filesystem paths and could solve this as well: + # from pathlib import Path + # region = Path(filename).stem.split('_')[-1] region = filename.split('_')[-1][:-4] - dataframe.mean().plot(ax=ax, label=region) + # pandas raises errors when it encounters non-numeric columns in a dataframe computation + # but we can tell pandas to ignore them with the `numeric_only` parameter + dataframe.mean(numeric_only=True).plot(ax=ax, label=region) + # NOTE: another way of doing this selects just the columns with gdp in their name using the filter method + # dataframe.filter(like="gdp").mean().plot(ax=ax, label=region) + plt.legend() plt.show() ``` @@ -231,8 +243,8 @@ gapminder_gdp_africa .csv ``` -**Hint:** It is possible to check all available attributes and methods on the `Path` object with the `dir()` -function! +**Hint:** Check all available attributes and methods on the `Path` object with the `dir()` +function. :::::::::::::::::::::::::::::::::::::::::::::::::: diff --git a/md5sum.txt b/md5sum.txt index 9a92e8fc5..4dcd8144a 100644 --- a/md5sum.txt +++ b/md5sum.txt @@ -17,7 +17,7 @@ "episodes/11-lists.md" "1257daeb542377a3b04c6bec0d0ffee1" "site/built/11-lists.md" "2023-07-24" "episodes/12-for-loops.md" "1da6e4e57a25f8d4fd64802c2eb682c4" "site/built/12-for-loops.md" "2023-05-02" "episodes/13-conditionals.md" "2739086f688f386c32ce56400c6b27e2" "site/built/13-conditionals.md" "2024-02-16" -"episodes/14-looping-data-sets.md" "e04f11544d1e5f3ca08ddcf22230a3a3" "site/built/14-looping-data-sets.md" "2023-05-02" +"episodes/14-looping-data-sets.md" "33bc3751e02186ba42ba35d937b03889" "site/built/14-looping-data-sets.md" "2024-03-05" "episodes/15-coffee.md" "062bae79eb17ee57f183b21658a8d813" "site/built/15-coffee.md" "2023-05-02" "episodes/16-writing-functions.md" "0f162f45b0072659b0113baf01ade027" "site/built/16-writing-functions.md" "2023-07-24" "episodes/17-scope.md" "8109afb18f278a482083d867ad80da6e" "site/built/17-scope.md" "2023-05-02"