Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

181118_myWeek1_Exercise

Views: 25
Kernel: Python 3 (Ubuntu Linux)

Exercise notebook 1: Having a go at it

This Jupyter notebook, for the first part of The Open University's Learn to Code for Data Analysis course, contains code examples and coding activities for you.

You'll come across steps in the course directing you to this notebook. Once you've done each exercise, go back to the corresponding step and mark it as complete.

# this code conceals irrelevant warning messages import warnings warnings.simplefilter('ignore', FutureWarning)

Exercise 1: variables and assignments

A variable is a named storage for values. An assignment takes a value (like the number 100 below) and stores it in a variable (deathsInPortugal below).

deathsInPortugal = 100

To display the value stored in a variable, write the name of the variable.

deathsInPortugal
100

Each variable can store one value at any time, but the value stored can vary over time, by assigning a new value to the variable.

deathsInPortugal = 100 deathsInPortugal = 140 deathsInPortugal
140

Each assignment is written on a separate line. The computer executes the assignments one line at a time, from top to bottom.

deathsInPortugal = 140 deathsInAngola = 6900 deathsInBrazil = 4400 deathsInRussia = 17000 deathsInIndia = 240000 deathsInChina = 41000 deathsInSouthAfrica = 25000

Task

Add assignments to the code cell above (or in a new code cell) for the estimated deaths by TB in 2013 in the remaining BRICS countries. The values are as follows: Russia 17000, India 240000, China 41000, South Africa 25000.

Don't forget to run the code cell, so that the new variables are available for the exercises further below.

Now go back to the course step and mark it complete.

Exercise 2: expressions

An expression is a fragment of code that has a value. A variable name, by itself, is an expression: the expression's value is the value stored in the variable. In Jupyter notebooks, if the last line of a code cell is an expression, then the computer will show its value when executing the cell.

deathsInPortugal
140

By contrast, a statement is a command for the computer to do something. Commands don't produce values, and therefore the computer doesn't display anything.

deathsInPortugal = 140

More complex expressions can be written using the arithmetic operators of addition (+), substraction (-), multiplication (*) and division (/). For example, the total number of deaths in the three countries is:

deathsInAngola + deathsInBrazil + deathsInPortugal
11440

If the calculated value needs to be used later on in the code, it has to be stored in a variable. In general, the right-hand side of an assignment is an expression; its value is calculated (the expression is evaluated) and stored in the variable.

totalDeaths = deathsInAngola + deathsInBrazil + deathsInPortugal totalDeaths
11440

The average number of deaths is the total divided by the number of countries.

totalDeaths / 3
3813.3333333333335

The average could also be calculated with a single expression.

(deathsInAngola + deathsInBrazil + deathsInPortugal) / 3
3813.3333333333335

The parentheses (round brackets) are necessary to state that the sum has to be calculated before the division. Without parentheses, Python follows the conventional order used in mathematics: divisions and multiplications are done before additions and subtractions.

deathsInAngola + deathsInBrazil + deathsInPortugal / 3
11346.666666666666

Task

  • In the cell below, write code to calculate the total number of deaths in the five BRICS countries (Brazil, Russia, India, China, South Africa) in 2013. Run the code to see the result, which should be 327400.

deathsInBrazil + deathsInRussia + deathsInIndia + deathsInChina + deathsInSouthAfrica
327400
  • In the cell below, write code to calculate the average number of deaths in the BRICS countries in 2013. Run the code to see the result, which should be 65480.

(deathsInBrazil + deathsInRussia + deathsInIndia + deathsInChina + deathsInSouthAfrica) / 5
65480.0

Now go back to the course step and mark it complete.

Exercise 3: functions quiz

A function takes zero or more values (the function's arguments) and returns (produces) a value. To call (use) a function, write the function name, followed by its arguments within parentheses (round brackets). Multiple arguments are separated by commas. Function names follow the same rules and conventions as variable names. A function call is an expression: the expression's value is the value returned by the function.

Python provides two functions to compute the maximum (largest) and minimum (smallest) of two or more values.

max(deathsInBrazil, deathsInPortugal)
4400
min(deathsInAngola, deathsInBrazil, deathsInPortugal)
140

The range of a set of values is the difference between the maximum and the minimum.

largest = max(deathsInBrazil, deathsInRussia, deathsInIndia, deathsInChina, deathsInSouthAfrica) smallest = min(deathsInBrazil, deathsInRussia, deathsInIndia, deathsInChina, deathsInSouthAfrica) deathsRange = largest - smallest deathsRange
235600

Tasks

Answer the quiz questions in the course. All of them can be answered by editing the above code cell. Don't forget that you can use TAB-completion to quickly write the variable names of the remaining BRICS countries, namely Russia, India, China and South Africa (Brazil is already in the code above).

Exercise 4: comments

Comments start with the hash sign (#) and go until the end of the line. They're used to annotate the code, e.g. to indicate the units of values.

# population unit: thousands of inhabitants populationOfBrazil = 200362 # deaths unit: inhabitants # deathsInPortugal = 140 # deaths per 100 thousand inhabitants deathsInBrazil * 100 / populationOfBrazil
2.1960251943981395

Task

Calculate the deaths per 100 thousand inhabitants for Brazil. Its population in 2013 was roughly 200 million and 362 thousand people. You should obtain a result of around 2.2 deaths per 100 thousand people.

Now go back to the course step and mark it complete.

Exercise 5: pandas quiz

All programs in this course must start with the following import statement, to load all the code from the pandas module.

from pandas import *

The words in boldface (from and import) are reserved words of the Python language; they cannot be used as names.

Task

Answer the quiz questions in the course. You can change the above line of code to find out the answers.

Exercise 6: selecting a column

The read_excel() function takes a string with the name of an Excel file, and returns a dataframe, the pandas representation of a table. The computer reports a file not found error if the file is not in the same folder as this notebook, or the file name is misspelt.

data = read_excel('WHO POP TB all.xls') data
Country Population (1000s) TB deaths
0 Afghanistan 30552 13000.00
1 Albania 3173 20.00
2 Algeria 39208 5100.00
3 Andorra 79 0.26
4 Angola 21472 6900.00
5 Antigua and Barbuda 90 1.20
6 Argentina 41446 570.00
7 Armenia 2977 170.00
8 Australia 23343 45.00
9 Austria 8495 29.00
10 Azerbaijan 9413 360.00
11 Bahamas 377 1.80
12 Bahrain 1332 9.60
13 Bangladesh 156595 80000.00
14 Barbados 285 2.00
15 Belarus 9357 850.00
16 Belgium 11104 18.00
17 Belize 332 20.00
18 Benin 10323 1300.00
19 Bhutan 754 88.00
20 Bolivia (Plurinational State of) 10671 430.00
21 Bosnia and Herzegovina 3829 190.00
22 Botswana 2021 440.00
23 Brazil 200362 4400.00
24 Brunei Darussalam 418 13.00
25 Bulgaria 7223 150.00
26 Burkina Faso 16935 1500.00
27 Burundi 10163 2300.00
28 Côte d'Ivoire 20316 4000.00
29 Cabo Verde 499 150.00
... ... ... ...
164 Suriname 539 12.00
165 Swaziland 1250 1100.00
166 Sweden 9571 13.00
167 Switzerland 8078 17.00
168 Syrian Arab Republic 21898 450.00
169 Tajikistan 8208 570.00
170 Thailand 67010 8100.00
171 The former Yugoslav republic of Macedonia 2107 33.00
172 Timor-Leste 1133 990.00
173 Togo 6817 810.00
174 Tonga 105 2.50
175 Trinidad and Tobago 1341 29.00
176 Tunisia 10997 230.00
177 Turkey 74933 310.00
178 Turkmenistan 5240 1300.00
179 Tuvalu 10 2.80
180 Uganda 37579 4100.00
181 Ukraine 45239 6600.00
182 United Arab Emirates 9346 64.00
183 United Kingdom of Great Britain and Northern I... 63136 340.00
184 United Republic of Tanzania 49253 6000.00
185 United States of America 320051 490.00
186 Uruguay 3407 40.00
187 Uzbekistan 28934 2200.00
188 Vanuatu 253 16.00
189 Venezuela (Bolivarian Republic of) 30405 480.00
190 Viet Nam 91680 17000.00
191 Yemen 24407 990.00
192 Zambia 14539 3600.00
193 Zimbabwe 14150 5700.00

194 rows × 3 columns

The expression dataFrame[columnName] evaluates to the column with the given name (a string). Column names are case sensitive. Misspelling the column name will result in a rather long key error message. You can see what happens by changing the string in the next code cell (e.g. replace TB by tb) and running it. Don't forget to undo your change and run the code again.

tbColumn = data['TB deaths'] tbColumn
0 13000.00 1 20.00 2 5100.00 3 0.26 4 6900.00 5 1.20 6 570.00 7 170.00 8 45.00 9 29.00 10 360.00 11 1.80 12 9.60 13 80000.00 14 2.00 15 850.00 16 18.00 17 20.00 18 1300.00 19 88.00 20 430.00 21 190.00 22 440.00 23 4400.00 24 13.00 25 150.00 26 1500.00 27 2300.00 28 4000.00 29 150.00 ... 164 12.00 165 1100.00 166 13.00 167 17.00 168 450.00 169 570.00 170 8100.00 171 33.00 172 990.00 173 810.00 174 2.50 175 29.00 176 230.00 177 310.00 178 1300.00 179 2.80 180 4100.00 181 6600.00 182 64.00 183 340.00 184 6000.00 185 490.00 186 40.00 187 2200.00 188 16.00 189 480.00 190 17000.00 191 990.00 192 3600.00 193 5700.00 Name: TB deaths, Length: 194, dtype: float64

Task

In the next cell, select the population column and store it in a variable (you'll use it in the next exercise). You need to scroll back to the start of the exercise to see the column's name.

tbPopulation = data['Population (1000s)'] tbPopulation
0 30552 1 3173 2 39208 3 79 4 21472 5 90 6 41446 7 2977 8 23343 9 8495 10 9413 11 377 12 1332 13 156595 14 285 15 9357 16 11104 17 332 18 10323 19 754 20 10671 21 3829 22 2021 23 200362 24 418 25 7223 26 16935 27 10163 28 20316 29 499 ... 164 539 165 1250 166 9571 167 8078 168 21898 169 8208 170 67010 171 2107 172 1133 173 6817 174 105 175 1341 176 10997 177 74933 178 5240 179 10 180 37579 181 45239 182 9346 183 63136 184 49253 185 320051 186 3407 187 28934 188 253 189 30405 190 91680 191 24407 192 14539 193 14150 Name: Population (1000s), Length: 194, dtype: int64
tbPopulation.max() - tbPopulation.min()
1393336

Now go back to the course step and mark it complete.

Exercise 7: calculations on a column

A method is a function that can only be called in a certain context, like a dataframe or a column. A method call is of the form context.methodName(argument1, argument2, ...).

Pandas provides several column methods, including to calculate the sum, the largest, and the smallest of the numbers in a column, as follows.

tbColumn.sum()
1072677.97
tbColumn.max()
240000.0
tbColumn.min()
0.0

The mean of a collection of numbers is the sum of those numbers divided by how many there are.

tbColumn.sum() / 12
89389.83083333333
tbColumn.mean()
5529.267886597938

The median of a collection of numbers is the number in the middle, i.e. half of the numbers are below the median and half are above.

tbColumn.median()
315.0

Tasks

Use the population column variable from the previous exercise to calculate:

  • the total population

tbPopulation.sum()
7126099
  • the maximum population

tbPopulation.max()
1393337
  • the minimum population

tbPopulation.min()
1

Now go back to the course step and mark it complete.

Exercise 8: sorting on a column

The dataframe method sort_values() takes as argument a column name and returns a new dataframe, with rows in ascending order according to the values in that column.

data.sort_values('TB deaths')
Country Population (1000s) TB deaths
147 San Marino 31 0.00
125 Niue 1 0.01
111 Monaco 38 0.03
3 Andorra 79 0.26
129 Palau 21 0.36
40 Cook Islands 21 0.41
118 Nauru 10 0.67
76 Iceland 330 0.93
68 Grenada 106 1.10
5 Antigua and Barbuda 90 1.20
113 Montenegro 621 1.20
152 Seychelles 93 1.40
105 Malta 429 1.50
143 Saint Kitts and Nevis 54 1.60
11 Bahamas 377 1.80
14 Barbados 285 2.00
144 Saint Lucia 182 2.20
99 Luxembourg 530 2.20
44 Cyprus 1141 2.30
174 Tonga 105 2.50
50 Dominica 72 2.70
137 Qatar 2169 2.70
179 Tuvalu 10 2.80
145 Saint Vincent and the Grenadines 109 3.10
126 Norway 5043 4.40
146 Samoa 190 6.10
121 New Zealand 4506 6.30
103 Maldives 345 7.60
12 Bahrain 1332 9.60
164 Suriname 539 12.00
... ... ... ...
160 South Sudan 11296 4500.00
119 Nepal 27797 4600.00
2 Algeria 39208 5100.00
193 Zimbabwe 14150 5700.00
184 United Republic of Tanzania 49253 6000.00
181 Ukraine 45239 6600.00
46 Democratic People's Republic of Korea 24895 6700.00
4 Angola 21472 6900.00
158 Somalia 10496 7700.00
31 Cameroon 22254 7800.00
170 Thailand 67010 8100.00
88 Kenya 44354 9100.00
163 Sudan 37964 9700.00
30 Cambodia 15135 10000.00
100 Madagascar 22925 12000.00
0 Afghanistan 30552 13000.00
141 Russian Federation 142834 17000.00
190 Viet Nam 91680 17000.00
115 Mozambique 25834 18000.00
159 South Africa 52776 25000.00
116 Myanmar 53259 26000.00
134 Philippines 98394 27000.00
58 Ethiopia 94101 30000.00
36 China 1393337 41000.00
47 Democratic Republic of the Congo 67514 46000.00
128 Pakistan 182143 49000.00
78 Indonesia 249866 64000.00
13 Bangladesh 156595 80000.00
124 Nigeria 173615 160000.00
77 India 1252140 240000.00

194 rows × 3 columns

Sorting doesn't change the original table.

data # rows still in original order
Country Population (1000s) TB deaths
0 Afghanistan 30552 13000.00
1 Albania 3173 20.00
2 Algeria 39208 5100.00
3 Andorra 79 0.26
4 Angola 21472 6900.00
5 Antigua and Barbuda 90 1.20
6 Argentina 41446 570.00
7 Armenia 2977 170.00
8 Australia 23343 45.00
9 Austria 8495 29.00
10 Azerbaijan 9413 360.00
11 Bahamas 377 1.80
12 Bahrain 1332 9.60
13 Bangladesh 156595 80000.00
14 Barbados 285 2.00
15 Belarus 9357 850.00
16 Belgium 11104 18.00
17 Belize 332 20.00
18 Benin 10323 1300.00
19 Bhutan 754 88.00
20 Bolivia (Plurinational State of) 10671 430.00
21 Bosnia and Herzegovina 3829 190.00
22 Botswana 2021 440.00
23 Brazil 200362 4400.00
24 Brunei Darussalam 418 13.00
25 Bulgaria 7223 150.00
26 Burkina Faso 16935 1500.00
27 Burundi 10163 2300.00
28 Côte d'Ivoire 20316 4000.00
29 Cabo Verde 499 150.00
... ... ... ...
164 Suriname 539 12.00
165 Swaziland 1250 1100.00
166 Sweden 9571 13.00
167 Switzerland 8078 17.00
168 Syrian Arab Republic 21898 450.00
169 Tajikistan 8208 570.00
170 Thailand 67010 8100.00
171 The former Yugoslav republic of Macedonia 2107 33.00
172 Timor-Leste 1133 990.00
173 Togo 6817 810.00
174 Tonga 105 2.50
175 Trinidad and Tobago 1341 29.00
176 Tunisia 10997 230.00
177 Turkey 74933 310.00
178 Turkmenistan 5240 1300.00
179 Tuvalu 10 2.80
180 Uganda 37579 4100.00
181 Ukraine 45239 6600.00
182 United Arab Emirates 9346 64.00
183 United Kingdom of Great Britain and Northern I... 63136 340.00
184 United Republic of Tanzania 49253 6000.00
185 United States of America 320051 490.00
186 Uruguay 3407 40.00
187 Uzbekistan 28934 2200.00
188 Vanuatu 253 16.00
189 Venezuela (Bolivarian Republic of) 30405 480.00
190 Viet Nam 91680 17000.00
191 Yemen 24407 990.00
192 Zambia 14539 3600.00
193 Zimbabwe 14150 5700.00

194 rows × 3 columns

Sorting on a column that has text will put the rows in alphabetical order.

data.sort_values('Country')
Country Population (1000s) TB deaths
0 Afghanistan 30552 13000.00
1 Albania 3173 20.00
2 Algeria 39208 5100.00
3 Andorra 79 0.26
4 Angola 21472 6900.00
5 Antigua and Barbuda 90 1.20
6 Argentina 41446 570.00
7 Armenia 2977 170.00
8 Australia 23343 45.00
9 Austria 8495 29.00
10 Azerbaijan 9413 360.00
11 Bahamas 377 1.80
12 Bahrain 1332 9.60
13 Bangladesh 156595 80000.00
14 Barbados 285 2.00
15 Belarus 9357 850.00
16 Belgium 11104 18.00
17 Belize 332 20.00
18 Benin 10323 1300.00
19 Bhutan 754 88.00
20 Bolivia (Plurinational State of) 10671 430.00
21 Bosnia and Herzegovina 3829 190.00
22 Botswana 2021 440.00
23 Brazil 200362 4400.00
24 Brunei Darussalam 418 13.00
25 Bulgaria 7223 150.00
26 Burkina Faso 16935 1500.00
27 Burundi 10163 2300.00
29 Cabo Verde 499 150.00
30 Cambodia 15135 10000.00
... ... ... ...
164 Suriname 539 12.00
165 Swaziland 1250 1100.00
166 Sweden 9571 13.00
167 Switzerland 8078 17.00
168 Syrian Arab Republic 21898 450.00
169 Tajikistan 8208 570.00
170 Thailand 67010 8100.00
171 The former Yugoslav republic of Macedonia 2107 33.00
172 Timor-Leste 1133 990.00
173 Togo 6817 810.00
174 Tonga 105 2.50
175 Trinidad and Tobago 1341 29.00
176 Tunisia 10997 230.00
177 Turkey 74933 310.00
178 Turkmenistan 5240 1300.00
179 Tuvalu 10 2.80
180 Uganda 37579 4100.00
181 Ukraine 45239 6600.00
182 United Arab Emirates 9346 64.00
183 United Kingdom of Great Britain and Northern I... 63136 340.00
184 United Republic of Tanzania 49253 6000.00
185 United States of America 320051 490.00
186 Uruguay 3407 40.00
187 Uzbekistan 28934 2200.00
188 Vanuatu 253 16.00
189 Venezuela (Bolivarian Republic of) 30405 480.00
190 Viet Nam 91680 17000.00
191 Yemen 24407 990.00
192 Zambia 14539 3600.00
193 Zimbabwe 14150 5700.00

194 rows × 3 columns

Task

Sort the same table by population, to quickly see which are the least and the most populous countries.

data.sort_values('Population (1000s)')
Country Population (1000s) TB deaths
125 Niue 1 0.01
179 Tuvalu 10 2.80
118 Nauru 10 0.67
40 Cook Islands 21 0.41
129 Palau 21 0.36
147 San Marino 31 0.00
111 Monaco 38 0.03
106 Marshall Islands 53 21.00
143 Saint Kitts and Nevis 54 1.60
50 Dominica 72 2.70
3 Andorra 79 0.26
5 Antigua and Barbuda 90 1.20
152 Seychelles 93 1.40
89 Kiribati 102 30.00
110 Micronesia (Federated States of) 104 22.00
174 Tonga 105 2.50
68 Grenada 106 1.10
145 Saint Vincent and the Grenadines 109 3.10
144 Saint Lucia 182 2.20
146 Samoa 190 6.10
148 Sao Tome and Principe 193 18.00
188 Vanuatu 253 16.00
14 Barbados 285 2.00
76 Iceland 330 0.93
17 Belize 332 20.00
103 Maldives 345 7.60
11 Bahamas 377 1.80
24 Brunei Darussalam 418 13.00
105 Malta 429 1.50
29 Cabo Verde 499 150.00
... ... ... ...
181 Ukraine 45239 6600.00
161 Spain 46927 240.00
37 Colombia 48321 770.00
184 United Republic of Tanzania 49253 6000.00
138 Republic of Korea 49263 2600.00
159 South Africa 52776 25000.00
116 Myanmar 53259 26000.00
83 Italy 60990 310.00
183 United Kingdom of Great Britain and Northern I... 63136 340.00
61 France 64291 340.00
170 Thailand 67010 8100.00
47 Democratic Republic of the Congo 67514 46000.00
177 Turkey 74933 310.00
79 Iran (Islamic Republic of) 77447 2500.00
53 Egypt 82056 550.00
65 Germany 82727 300.00
190 Viet Nam 91680 17000.00
58 Ethiopia 94101 30000.00
134 Philippines 98394 27000.00
109 Mexico 122332 2200.00
85 Japan 127144 2100.00
141 Russian Federation 142834 17000.00
13 Bangladesh 156595 80000.00
124 Nigeria 173615 160000.00
128 Pakistan 182143 49000.00
23 Brazil 200362 4400.00
78 Indonesia 249866 64000.00
185 United States of America 320051 490.00
77 India 1252140 240000.00
36 China 1393337 41000.00

194 rows × 3 columns

Now go back to the course step and mark it complete.

Final quiz: Calculations over columns

This information will help you to answer questions in the final quiz.

The value of an arithmetic expression involving columns is a column. In evaluating the expression, the computer computes the expression for each row.

deathsColumn = data['TB deaths'] populationColumn = data['Population (1000s)'] rateColumn = deathsColumn * 100 / populationColumn rateColumn
0 42.550406 1 0.630318 2 13.007549 3 0.329114 4 32.134873 5 1.333333 6 1.375284 7 5.710447 8 0.192777 9 0.341377 10 3.824498 11 0.477454 12 0.720721 13 51.087199 14 0.701754 15 9.084108 16 0.162104 17 6.024096 18 12.593238 19 11.671088 20 4.029613 21 4.962131 22 21.771400 23 2.196025 24 3.110048 25 2.076699 26 8.857396 27 22.631113 28 19.688915 29 30.060120 ... 164 2.226345 165 88.000000 166 0.135827 167 0.210448 168 2.054982 169 6.944444 170 12.087748 171 1.566208 172 87.378641 173 11.882060 174 2.380952 175 2.162565 176 2.091479 177 0.413703 178 24.809160 179 28.000000 180 10.910349 181 14.589182 182 0.684785 183 0.538520 184 12.181999 185 0.153101 186 1.174053 187 7.603511 188 6.324111 189 1.578688 190 18.542757 191 4.056213 192 24.760988 193 40.282686 Length: 194, dtype: float64

To add a new column to a dataframe, 'select' a non-existing column, i.e. with a new name, and assign to it.

data['TB deaths (per 100000)'] = rateColumn data
Country Population (1000s) TB deaths TB deaths (per 100000)
0 Afghanistan 30552 13000.00 42.550406
1 Albania 3173 20.00 0.630318
2 Algeria 39208 5100.00 13.007549
3 Andorra 79 0.26 0.329114
4 Angola 21472 6900.00 32.134873
5 Antigua and Barbuda 90 1.20 1.333333
6 Argentina 41446 570.00 1.375284
7 Armenia 2977 170.00 5.710447
8 Australia 23343 45.00 0.192777
9 Austria 8495 29.00 0.341377
10 Azerbaijan 9413 360.00 3.824498
11 Bahamas 377 1.80 0.477454
12 Bahrain 1332 9.60 0.720721
13 Bangladesh 156595 80000.00 51.087199
14 Barbados 285 2.00 0.701754
15 Belarus 9357 850.00 9.084108
16 Belgium 11104 18.00 0.162104
17 Belize 332 20.00 6.024096
18 Benin 10323 1300.00 12.593238
19 Bhutan 754 88.00 11.671088
20 Bolivia (Plurinational State of) 10671 430.00 4.029613
21 Bosnia and Herzegovina 3829 190.00 4.962131
22 Botswana 2021 440.00 21.771400
23 Brazil 200362 4400.00 2.196025
24 Brunei Darussalam 418 13.00 3.110048
25 Bulgaria 7223 150.00 2.076699
26 Burkina Faso 16935 1500.00 8.857396
27 Burundi 10163 2300.00 22.631113
28 Côte d'Ivoire 20316 4000.00 19.688915
29 Cabo Verde 499 150.00 30.060120
... ... ... ... ...
164 Suriname 539 12.00 2.226345
165 Swaziland 1250 1100.00 88.000000
166 Sweden 9571 13.00 0.135827
167 Switzerland 8078 17.00 0.210448
168 Syrian Arab Republic 21898 450.00 2.054982
169 Tajikistan 8208 570.00 6.944444
170 Thailand 67010 8100.00 12.087748
171 The former Yugoslav republic of Macedonia 2107 33.00 1.566208
172 Timor-Leste 1133 990.00 87.378641
173 Togo 6817 810.00 11.882060
174 Tonga 105 2.50 2.380952
175 Trinidad and Tobago 1341 29.00 2.162565
176 Tunisia 10997 230.00 2.091479
177 Turkey 74933 310.00 0.413703
178 Turkmenistan 5240 1300.00 24.809160
179 Tuvalu 10 2.80 28.000000
180 Uganda 37579 4100.00 10.910349
181 Ukraine 45239 6600.00 14.589182
182 United Arab Emirates 9346 64.00 0.684785
183 United Kingdom of Great Britain and Northern I... 63136 340.00 0.538520
184 United Republic of Tanzania 49253 6000.00 12.181999
185 United States of America 320051 490.00 0.153101
186 Uruguay 3407 40.00 1.174053
187 Uzbekistan 28934 2200.00 7.603511
188 Vanuatu 253 16.00 6.324111
189 Venezuela (Bolivarian Republic of) 30405 480.00 1.578688
190 Viet Nam 91680 17000.00 18.542757
191 Yemen 24407 990.00 4.056213
192 Zambia 14539 3600.00 24.760988
193 Zimbabwe 14150 5700.00 40.282686

194 rows × 4 columns

data.sort_values('TB deaths (per 100000)')
Country Population (1000s) TB deaths TB deaths (per 100000)
147 San Marino 31 0.00 0.000000
111 Monaco 38 0.03 0.078947
126 Norway 5043 4.40 0.087250
120 Netherlands 16759 20.00 0.119339
137 Qatar 2169 2.70 0.124481
166 Sweden 9571 13.00 0.135827
121 New Zealand 4506 6.30 0.139814
185 United States of America 320051 490.00 0.153101
16 Belgium 11104 18.00 0.162104
32 Canada 35182 62.00 0.176226
8 Australia 23343 45.00 0.192777
113 Montenegro 621 1.20 0.193237
44 Cyprus 1141 2.30 0.201578
82 Israel 7733 16.00 0.206905
167 Switzerland 8078 17.00 0.210448
45 Czech Republic 10702 28.00 0.261633
76 Iceland 330 0.93 0.281818
60 Finland 5426 17.00 0.313306
43 Cuba 11266 37.00 0.328422
3 Andorra 79 0.26 0.329114
9 Austria 8495 29.00 0.341377
105 Malta 429 1.50 0.349650
65 Germany 82727 300.00 0.362639
81 Ireland 4627 18.00 0.389021
177 Turkey 74933 310.00 0.413703
99 Luxembourg 530 2.20 0.415094
48 Denmark 5619 24.00 0.427122
11 Bahamas 377 1.80 0.477454
86 Jordan 7274 35.00 0.481166
83 Italy 60990 310.00 0.508280
... ... ... ... ...
29 Cabo Verde 499 150.00 30.060120
58 Ethiopia 94101 30000.00 31.880639
4 Angola 21472 6900.00 32.134873
131 Papua New Guinea 7321 2400.00 32.782407
31 Cameroon 22254 7800.00 35.049879
106 Marshall Islands 53 21.00 39.622642
160 South Sudan 11296 4500.00 39.837110
193 Zimbabwe 14150 5700.00 40.282686
0 Afghanistan 30552 13000.00 42.550406
153 Sierra Leone 6092 2600.00 42.678923
39 Congo 4448 2000.00 44.964029
95 Lesotho 2074 960.00 46.287367
159 South Africa 52776 25000.00 47.370017
33 Central African Republic 4616 2200.00 47.660312
116 Myanmar 53259 26000.00 48.818040
96 Liberia 4294 2100.00 48.905449
13 Bangladesh 156595 80000.00 51.087199
100 Madagascar 22925 12000.00 52.344602
92 Lao People's Democratic Republic 6770 3600.00 53.175775
62 Gabon 1672 910.00 54.425837
117 Namibia 2303 1300.00 56.448111
30 Cambodia 15135 10000.00 66.072019
47 Democratic Republic of the Congo 67514 46000.00 68.134017
115 Mozambique 25834 18000.00 69.675621
71 Guinea-Bissau 1704 1200.00 70.422535
158 Somalia 10496 7700.00 73.361280
172 Timor-Leste 1133 990.00 87.378641
165 Swaziland 1250 1100.00 88.000000
124 Nigeria 173615 160000.00 92.157936
49 Djibouti 873 870.00 99.656357

194 rows × 4 columns

Tasks

Add code to calculate:

  • the range of the population, in thousands of inhabitants

deathsColumn = data['TB deaths'] populationColumn = data['Population (1000s)'] rateColumn = deathsColumn * 100 / populationColumn rateColumn
0 42.550406 1 0.630318 2 13.007549 3 0.329114 4 32.134873 5 1.333333 6 1.375284 7 5.710447 8 0.192777 9 0.341377 10 3.824498 11 0.477454 12 0.720721 13 51.087199 14 0.701754 15 9.084108 16 0.162104 17 6.024096 18 12.593238 19 11.671088 20 4.029613 21 4.962131 22 21.771400 23 2.196025 24 3.110048 25 2.076699 26 8.857396 27 22.631113 28 19.688915 29 30.060120 ... 164 2.226345 165 88.000000 166 0.135827 167 0.210448 168 2.054982 169 6.944444 170 12.087748 171 1.566208 172 87.378641 173 11.882060 174 2.380952 175 2.162565 176 2.091479 177 0.413703 178 24.809160 179 28.000000 180 10.910349 181 14.589182 182 0.684785 183 0.538520 184 12.181999 185 0.153101 186 1.174053 187 7.603511 188 6.324111 189 1.578688 190 18.542757 191 4.056213 192 24.760988 193 40.282686 Length: 194, dtype: float64
  • the mean of the death rate

rateColumn.mean()
13.988003982547117
  • the median of the death rate

rateColumn.median()
4.223028743152806

Now you can answer the questions in the final quiz.