Skip to content

Commit

Permalink
Added list comprehension lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
rlbarter committed Jan 30, 2024
1 parent c0117af commit 411e332
Show file tree
Hide file tree
Showing 5 changed files with 2,471 additions and 64 deletions.
106 changes: 48 additions & 58 deletions content/complete/08_series.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# import the pandas library\n",
"import pandas as pd"
"import pandas as pd\n",
"import numpy as np"
]
},
{
Expand Down Expand Up @@ -551,19 +552,29 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'np' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[13], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Try to compute the logarithm of the gdpPercap column of gapminder\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mnp\u001b[49m\u001b[38;5;241m.\u001b[39mlog(gapminder[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgdpPercap\u001b[39m\u001b[38;5;124m'\u001b[39m])\n",
"\u001b[0;31mNameError\u001b[0m: name 'np' is not defined"
]
"data": {
"text/plain": [
"0 6.658583\n",
"1 6.710344\n",
"2 6.748878\n",
"3 6.728864\n",
"4 6.606625\n",
" ... \n",
"1699 6.559838\n",
"1700 6.541637\n",
"1701 6.675129\n",
"1702 6.510316\n",
"1703 6.152114\n",
"Name: gdpPercap, Length: 1704, dtype: float64"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
Expand All @@ -587,7 +598,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand All @@ -607,7 +618,7 @@
"Name: year, Length: 1704, dtype: bool"
]
},
"execution_count": 14,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -626,7 +637,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand All @@ -646,7 +657,7 @@
"Name: lifeExp, Length: 1704, dtype: bool"
]
},
"execution_count": 15,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -669,12 +680,12 @@
"source": [
"### Exercise\n",
"\n",
"1. Extract the `country` and `continent` columns from gapminder and create a Series object that contains the country and continent values separated by a comma, e.g., the first few entries should be \"Afghanistan, Asia\"."
"1. Extract the `country` and `continent` columns from gapminder and create a Series object that contains the country and continent values separated by a comma, e.g., the first few entries should be \"Afghanistan, Asia\". As an added challenge, use the `drop_duplicates()` Pandas method to ensure that you only have unique values in your output."
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 18,
"metadata": {},
"outputs": [
{
Expand All @@ -683,7 +694,7 @@
"'ab'"
]
},
"execution_count": 16,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -695,34 +706,35 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 Afghanistan, Asia\n",
"1 Afghanistan, Asia\n",
"2 Afghanistan, Asia\n",
"3 Afghanistan, Asia\n",
"4 Afghanistan, Asia\n",
" ... \n",
"1699 Zimbabwe, Africa\n",
"1700 Zimbabwe, Africa\n",
"1701 Zimbabwe, Africa\n",
"1702 Zimbabwe, Africa\n",
"1703 Zimbabwe, Africa\n",
"Length: 1704, dtype: object"
"0 Afghanistan, Asia\n",
"12 Albania, Europe\n",
"24 Algeria, Africa\n",
"36 Angola, Africa\n",
"48 Argentina, Americas\n",
" ... \n",
"1644 Vietnam, Asia\n",
"1656 West Bank and Gaza, Asia\n",
"1668 Yemen, Rep., Asia\n",
"1680 Zambia, Africa\n",
"1692 Zimbabwe, Africa\n",
"Length: 142, dtype: object"
]
},
"execution_count": 17,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create a series that contains the country and continent for each row separated by a comma\n",
"gapminder['country'] + ', ' + gapminder['continent']"
"country_continent = gapminder['country'] + ', ' + gapminder['continent']\n",
"country_continent.drop_duplicates()"
]
},
{
Expand All @@ -734,31 +746,9 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 6.567086e+09\n",
"1 7.585449e+09\n",
"2 8.758856e+09\n",
"3 9.648014e+09\n",
"4 9.678553e+09\n",
" ... \n",
"1699 6.508241e+09\n",
"1700 7.422612e+09\n",
"1701 9.037851e+09\n",
"1702 8.015111e+09\n",
"1703 5.782658e+09\n",
"Length: 1704, dtype: float64"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"# create a series object containing the GDP by multiplying pop by gdpPercap\n",
"gapminder['pop'] * gapminder['gdpPercap']"
Expand Down
2 changes: 1 addition & 1 deletion content/complete/12_iloc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### More general sequencing with `start:stop:step`\n",
"### More general sequences with `start:stop:step`\n",
"\n",
"And finally, you can extract more general sequences of rows/columns using the `start:stop` sequencing syntax. `0:20` will correspond to a list of integers from 0 up to 20 (not inclusive -- so it will actually go up to 19). \n",
"\n",
Expand Down
Loading

0 comments on commit 411e332

Please sign in to comment.