-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathj2sr_03_quarto_python-functions.qmd
More file actions
108 lines (72 loc) · 2.81 KB
/
Copy pathj2sr_03_quarto_python-functions.qmd
File metadata and controls
108 lines (72 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---
title: "Write functions"
format: html
---
# Instructions
This week, you learned how to write and apply your own functions.
You'll use these skills to add a column that recodes income groups into their full, more human-readable, names.
To learn more about the variables contained in the data set,
refer to the data dictionary [here](https://rstudio.github.io/academy-python-foreign-aid/assets/j2sr_dictionary.html).
# Milestone
```{python}
#| label: 'setup'
#| include: false
# Import your packages here
import pandas as pd
from matplotlib import rcParams
# Set some options
pd.set_option('display.max_columns', None)
rcParams.update({'savefig.bbox': 'tight'}) # Keeps plotnine legend from being cut off
```
## Write functions
As we work towards building polished reports and plots,
it may be useful to include the full name of income groups within our data set rather than the abbreviation.
In this milestone,
you'll create a new variable that contains the full names associated with each income group.
## Recreation
### Part 1 - Import
Before you begin, you will need to import your data set.
Use the code chunk below to read the data from the data file `j2sr.csv`,
which is stored in the `data/` folder in your working directory.
Be sure to save the data set to a variable named `j2sr`.
```{python}
#| label: 'recreation_import'
```
### Part 2 - Write a function
Your task is to write a function named `recode_income()` that will accept a single string value as input (e.g. `"LMIC"`)
and return the associated name:
- 'LIC': 'Low income'
- 'LMIC': 'Lower-middle income'
- 'UMIC': 'Upper-middle income'
- 'HIC': 'High income'
```{python}
#| label: 'recreation-function'
```
#### Test your function
Run the following code chunk to test your function on a few different values:
```{python}
#| label: 'compare_results'
recode_income('LIC') # should return 'Low income'
recode_income('LMIC') # should return 'Lower-middle income'
recode_income('UMIC') # should return 'Upper-middle income'
```
### Part 3 - Apply
Using your function, add a new column to `j2sr` called `income_group_name`
that contains the name associated with each income group.
```{python}
#| label: 'recreation_apply'
```
## Extension
Use the code chunk(s) below to extend your work.
Try something that you can learn from experimentation, a help page, or a package website.
Consider:
1. Writing a function to perform a calculation involving one or more columns of `j2sr`
2. Writing a function that subsets the data set for a given country or region and creates a visualization
3. Something else
Alternately, working with a data set of your own, complete the following:
1. Read in your data
2. Write a function that performs an operation on your data
3. Call the function, or use `.apply()` to apply it to every row in the dataset
```{python}
#| label: 'extension'
```