diff --git a/2 - Pandas Series exercises.ipynb b/2 - Pandas Series exercises.ipynb index 4213ac6..7947e7d 100644 --- a/2 - Pandas Series exercises.ipynb +++ b/2 - Pandas Series exercises.ipynb @@ -1,947 +1,1409 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", - "
\n", - "\n", - "# Pandas Series exercises\n" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0.25.3\n" - ] + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.4" + }, + "colab": { + "name": "2 - Pandas Series exercises.ipynb", + "provenance": [] } - ], - "source": [ - "# Import the numpy package under the name np\n", - "import numpy as np\n", - "\n", - "# Import the pandas package under the name pd\n", - "import pandas as pd\n", - "\n", - "# Print the pandas version and the configuration\n", - "print(pd.__version__)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Series creation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Create an empty pandas Series" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "pd.Series()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X python list convert it to an Y pandas Series" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = ['A','B','C']\n", - "print(X, type(X))\n", - "\n", - "Y = pd.Series(X)\n", - "print(Y, type(Y)) # different type" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, name it 'My letters'" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series(['A','B','C'])\n", - "\n", - "X.name = 'My letters'\n", - "X" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, show its values\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series(['A','B','C'])\n", - "\n", - "X.values" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Series indexation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Assign index names to the given X pandas Series\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series(['A','B','C'])\n", - "index_names = ['first', 'second', 'third']\n", - "\n", - "X.index = index_names\n", - "X" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, show its first element\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series(['A','B','C'], index=['first', 'second', 'third'])\n", - "\n", - "#X[0] # by position\n", - "#X.iloc[0] # by position\n", - "X['first'] # by index" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, show its last element\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series(['A','B','C'], index=['first', 'second', 'third'])\n", - "\n", - "#X[-1] # by position\n", - "#X.iloc[-1] # by position\n", - "X['third'] # by index" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, show all middle elements\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series(['A','B','C','D','E'],\n", - " index=['first','second','third','forth','fifth'])\n", - "\n", - "#X[['second', 'third', 'forth']]\n", - "#X.iloc[1:-1] # by position\n", - "X[1:-1] # by position" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, show the elements in reverse position\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series(['A','B','C','D','E'],\n", - " index=['first','second','third','forth','fifth'])\n", - "\n", - "#X.iloc[::-1]\n", - "X[::-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, show the first and last elements\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series(['A','B','C','D','E'],\n", - " index=['first','second','third','forth','fifth'])\n", - "\n", - "#X[['first', 'fifth']]\n", - "#X.iloc[[0, -1]]\n", - "X[[0, -1]]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Series manipulation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Convert the given integer pandas Series to float\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([1,2,3,4,5],\n", - " index=['first','second','third','forth','fifth'])\n", - "\n", - "pd.Series(X, dtype=np.float)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Reverse the given pandas Series (first element becomes last)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([1,2,3,4,5],\n", - " index=['first','second','third','forth','fifth'])\n", - "\n", - "X[::-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Order (sort) the given pandas Series\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([4,2,5,1,3],\n", - " index=['forth','second','fifth','first','third'])\n", - "\n", - "X = X.sort_values()\n", - "X" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, set the fifth element equal to 10\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([1,2,3,4,5],\n", - " index=['A','B','C','D','E'])\n", - "\n", - "X[4] = 10\n", - "X" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, change all the middle elements to 0\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution", - "scrolled": false - }, - "outputs": [], - "source": [ - "X = pd.Series([1,2,3,4,5],\n", - " index=['A','B','C','D','E'])\n", - "\n", - "X[1:-1] = 0\n", - "X" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, add 5 to every element\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([1,2,3,4,5])\n", - "\n", - "X + 5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Series boolean arrays (also called masks)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Given the X pandas Series, make a mask showing negative elements\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([-1,2,0,-4,5,6,0,0,-9,10])\n", - "\n", - "mask = X <= 0\n", - "mask" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, get the negative elements\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([-1,2,0,-4,5,6,0,0,-9,10])\n", - "\n", - "mask = X <= 0\n", - "X[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, get numbers higher than 5\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([-1,2,0,-4,5,6,0,0,-9,10])\n", - "\n", - "mask = X > 5\n", - "X[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, get numbers higher than the elements mean" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([-1,2,0,-4,5,6,0,0,-9,10])\n", - "\n", - "mask = X > X.mean()\n", - "X[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, get numbers equal to 2 or 10\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution", - "scrolled": true - }, - "outputs": [], - "source": [ - "X = pd.Series([-1,2,0,-4,5,6,0,0,-9,10])\n", - "\n", - "mask = (X == 2) | (X == 10)\n", - "X[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Logic functions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Given the X pandas Series, return True if none of its elements is zero" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([-1,2,0,-4,5,6,0,0,-9,10])\n", - "\n", - "X.all()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, return True if any of its elements is zero\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([-1,2,0,-4,5,6,0,0,-9,10])\n", - "\n", - "X.any()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Summary statistics" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Given the X pandas Series, show the sum of its elements\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([3,5,6,7,2,3,4,9,4])\n", - "\n", - "#np.sum(X)\n", - "X.sum()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, show the mean value of its elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([1,2,0,4,5,6,0,0,9,10])\n", - "\n", - "#np.mean(X)\n", - "X.mean()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X pandas Series, show the max value of its elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = pd.Series([1,2,0,4,5,6,0,0,9,10])\n", - "\n", - "#np.max(X)\n", - "X.max()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.7.4" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "ybDWVcMjbMp-" + }, + "source": [ + "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", + "
\n", + "\n", + "# Pandas Series exercises\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Tb2rsdVpbMqH", + "outputId": "6ab94c1f-4e97-46e9-db1a-e10a2bb3dbcc" + }, + "source": [ + "# Import the numpy package under the name np\n", + "import numpy as np\n", + "\n", + "# Import the pandas package under the name pd\n", + "import pandas as pd\n", + "\n", + "# Print the pandas version and the configuration\n", + "print(pd.__version__)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0.25.3\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ml8toXCFbMqK" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Series creation" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VmFbr-xEbeXI" + }, + "source": [ + "X = ['A','B','C']\r\n", + "\r\n", + "# use 'x' to solve problems\r\n", + "x = X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Mnp8dk16bMqK" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\r\n", + "\r\n", + "### Create an empty pandas Series" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "DhXlWbsgbMqL" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BdvIUcaXbODi" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "bMkmWO9qbMqM" + }, + "source": [ + "pd.Series()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6c_sZxmHbMqN" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X python list convert it to an Y pandas Series" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "arwXpucJbMqO" + }, + "source": [ + "print(X, type(X))\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wjbtaFKibUL_" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "36yq8IyybMqO" + }, + "source": [ + "print(X, type(X))\n", + "\n", + "Y = pd.Series(X)\n", + "print(Y, type(Y)) # different type" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gq-niNjHbMqQ" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, name it 'My letters'" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "m0DwoDRKbMqR" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BEByVL9Ob6RY" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "O57B0DwbbMqS" + }, + "source": [ + "X.name = 'My letters'\n", + "X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ru93Ox4mbMqS" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, show its values\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "EeKlQ0xtbMqT" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "R5CKr2V0cD0V" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "jw9jjOkmbMqT" + }, + "source": [ + "X.values" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EQScgSBGbMqT" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Series indexation" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "elLEOculbMqT" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Assign index names to the given X pandas Series\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "C3lUoLxvbMqU" + }, + "source": [ + "x = pd.Series(['A','B','C'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6oGaFTl2cOs_" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "5xyTYknCbMqU" + }, + "source": [ + "X = pd.Series(['A','B','C'])\n", + "index_names = ['first', 'second', 'third']\n", + "\n", + "X.index = index_names\n", + "X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8dZFx4Z_bMqV" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, show its first element\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "gFfs9OVcbMqV" + }, + "source": [ + "x = pd.Series(['A','B','C'], index=['first', 'second', 'third'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8DGtME8YdVpH" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "i_Lu5xR0bMqV" + }, + "source": [ + "X = pd.Series(['A','B','C'], index=['first', 'second', 'third'])\n", + "\n", + "#X[0] # by position\n", + "#X.iloc[0] # by position\n", + "X['first'] # by index" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RytSngYSbMqV" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, show its last element\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1teZ4A5JbMqW" + }, + "source": [ + "x = pd.Series(['A','B','C'], index=['first', 'second', 'third'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "D1XRYspTdZTl" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "_43opeJPbMqW" + }, + "source": [ + "X = pd.Series(['A','B','C'], index=['first', 'second', 'third'])\n", + "\n", + "#X[-1] # by position\n", + "#X.iloc[-1] # by position\n", + "X['third'] # by index" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "m-JWkFAubMqW" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, show all middle elements\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "GjPJekJkbMqW" + }, + "source": [ + "x = pd.Series(['A','B','C','D','E'],\n", + " index=['first','second','third','forth','fifth'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fOQFJHmMdZ3-" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "WCpl4DD3bMqX" + }, + "source": [ + "X = pd.Series(['A','B','C','D','E'],\n", + " index=['first','second','third','forth','fifth'])\n", + "\n", + "#X[['second', 'third', 'forth']]\n", + "#X.iloc[1:-1] # by position\n", + "X[1:-1] # by position" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mJy-1hXEbMqX" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, show the elements in reverse position\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "x_cKQKyebMqX" + }, + "source": [ + "x = pd.Series(['A','B','C','D','E'],\n", + " index=['first','second','third','forth','fifth'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zSqp7hqHdafW" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "lw-u_eS-bMqX" + }, + "source": [ + "X = pd.Series(['A','B','C','D','E'],\n", + " index=['first','second','third','forth','fifth'])\n", + "\n", + "#X.iloc[::-1]\n", + "X[::-1]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mnhnni46bMqY" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, show the first and last elements\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "y_O1oQXJbMqY" + }, + "source": [ + "x = pd.Series(['A','B','C','D','E'],\n", + " index=['first','second','third','forth','fifth'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jaBPE0kQdbKC" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "w7RqwiGSbMqY" + }, + "source": [ + "X = pd.Series(['A','B','C','D','E'],\n", + " index=['first','second','third','forth','fifth'])\n", + "\n", + "#X[['first', 'fifth']]\n", + "#X.iloc[[0, -1]]\n", + "X[[0, -1]]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "D401INVMbMqY" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Series manipulation" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GdTJdnaPbMqY" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Convert the given integer pandas Series to float\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4YJV0Wb6bMqZ" + }, + "source": [ + "x = pd.Series([1,2,3,4,5],\n", + " index=['first','second','third','forth','fifth'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yQDnY3JOeIem" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "RMOpQWZrbMqZ" + }, + "source": [ + "X = pd.Series([1,2,3,4,5],\n", + " index=['first','second','third','forth','fifth'])\n", + "\n", + "pd.Series(X, dtype=np.float)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SMkdBcCHbMqZ" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Reverse the given pandas Series (first element becomes last)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kRig0FuhbMqZ" + }, + "source": [ + "x = pd.Series([1,2,3,4,5],\n", + " index=['first','second','third','forth','fifth'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rPT148wheMSI" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "l4sqpLlFbMqZ" + }, + "source": [ + "X = pd.Series([1,2,3,4,5],\n", + " index=['first','second','third','forth','fifth'])\n", + "\n", + "X[::-1]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bxHFoFW3bMqa" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Order (sort) the given pandas Series\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Vux1YcBbbMqa" + }, + "source": [ + "x = pd.Series([4,2,5,1,3],\n", + " index=['forth','second','fifth','first','third'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CchPBdy6eNLO" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "EJUe--SQbMqa" + }, + "source": [ + "X = pd.Series([4,2,5,1,3],\n", + " index=['forth','second','fifth','first','third'])\n", + "\n", + "X = X.sort_values()\n", + "X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TCWeGylXbMqa" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, set the fifth element equal to 10\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ni3lwmW0bMqb" + }, + "source": [ + "x = pd.Series([1,2,3,4,5],\n", + " index=['A','B','C','D','E'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sKlH-pBheN99" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "NEVGeUaAbMqb" + }, + "source": [ + "X = pd.Series([1,2,3,4,5],\n", + " index=['A','B','C','D','E'])\n", + "\n", + "X[4] = 10\n", + "X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OWKdO32ObMqb" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, change all the middle elements to 0\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HhNhNsLJbMqc" + }, + "source": [ + "x = pd.Series([1,2,3,4,5],\n", + " index=['A','B','C','D','E'])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IrxnrTM3eOib" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "scrolled": false, + "id": "xxXXDPiVbMqc" + }, + "source": [ + "X = pd.Series([1,2,3,4,5],\n", + " index=['A','B','C','D','E'])\n", + "\n", + "X[1:-1] = 0\n", + "X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nZjglA13bMqd" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, add 5 to every element\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "2T0U_0sHbMqd" + }, + "source": [ + "x = pd.Series([1,2,3,4,5])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3GoKzNRSePab" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "gRwmwUnFbMqd" + }, + "source": [ + "X = pd.Series([1,2,3,4,5])\n", + "\n", + "X + 5" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HYPkf8HJbMqd" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Series boolean arrays (also called masks)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bIYZpiQxe3GS" + }, + "source": [ + "X = pd.Series([-1,2,0,-4,5,6,0,0,-9,10])\r\n", + "\r\n", + "# use 'x' to solve problems\r\n", + "x = X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5nUM2Vf5bMqd" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, make a mask showing negative elements\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "q0JRN0FfbMqe" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1qSGhmzGenIm" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "XegTI9x1bMqe" + }, + "source": [ + "mask = X <= 0\n", + "mask" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NkN2soUBbMqe" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, get the negative elements\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "dIcxoX5HbMqe" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "93YXd25pevvO" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "eH4gpztWbMqe" + }, + "source": [ + "mask = X <= 0\n", + "X[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5gpLGeeubMqf" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, get numbers higher than 5\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "B05uJE7_bMqh" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "S6-hhTKeevNd" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "zp5VBBBBbMqh" + }, + "source": [ + "mask = X > 5\n", + "X[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-eKYq1zDbMqi" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, get numbers higher than the elements mean" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "q1H1M5jubMqi" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aKONvl9zeubD" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "aDH5To4NbMqi" + }, + "source": [ + "mask = X > X.mean()\n", + "X[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6cavoIB2bMqi" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, get numbers equal to 2 or 10\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XgrO__PnbMqi" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E4xY48mJet5t" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "scrolled": true, + "id": "sV85mMA6bMqj" + }, + "source": [ + "mask = (X == 2) | (X == 10)\n", + "X[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pCeYr7oqbMqj" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Logic functions" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "vQocn2QkfT91" + }, + "source": [ + "X = pd.Series([-1,2,0,-4,5,6,0,0,-9,10])\r\n", + "\r\n", + "# use 'x' to solve problems\r\n", + "x = X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yxG57piJbMqj" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\r\n", + "\r\n", + "### Given the X pandas Series, return True if none of its elements is zero" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "CePlf6lJbMqj" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b566GNvofbgC" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "GXKWqHiGbMqk" + }, + "source": [ + "X.all()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ukCqpMnVbMqk" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, return True if any of its elements is zero\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JX7hp9DsbMqk" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qE_qyWCqffhD" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "F8KfQk7RbMql" + }, + "source": [ + "X.any()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "q14CVR36bMql" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Summary statistics" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wRYj5CTdbMqm" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, show the sum of its elements\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "vO8LBf-PbMqm" + }, + "source": [ + "x = pd.Series([3,5,6,7,2,3,4,9,4])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hJIkZCrAgSJB" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "t7Zq1ZlmbMqn" + }, + "source": [ + "X = pd.Series([3,5,6,7,2,3,4,9,4])\n", + "\n", + "#np.sum(X)\n", + "X.sum()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xEjk7RD6bMqn" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, show the mean value of its elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A0ukQoVzbMqn" + }, + "source": [ + "X = pd.Series([1,2,0,4,5,6,0,0,9,10])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yZsLR6-fgRis" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "LB4JD1i0bMqo" + }, + "source": [ + "X = pd.Series([1,2,0,4,5,6,0,0,9,10])\n", + "\n", + "#np.mean(X)\n", + "X.mean()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zr9_DadCbMqo" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X pandas Series, show the max value of its elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9Lx7RuPObMqo" + }, + "source": [ + "X = pd.Series([1,2,0,4,5,6,0,0,9,10])\n", + "\n", + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pdHkpDwigPA2" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "yXsuU4OqbMqo" + }, + "source": [ + "X = pd.Series([1,2,0,4,5,6,0,0,9,10])\n", + "\n", + "#np.max(X)\n", + "X.max()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fVDWb1sZbMqo" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" + ] + } + ] +} \ No newline at end of file diff --git a/4 - Pandas DataFrames exercises.ipynb b/4 - Pandas DataFrames exercises.ipynb index 4227f4d..d11d8ee 100644 --- a/4 - Pandas DataFrames exercises.ipynb +++ b/4 - Pandas DataFrames exercises.ipynb @@ -1,882 +1,1243 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", - "
\n", - "\n", - "# Pandas DataFrame exercises\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Import the numpy package under the name np\n", - "import numpy as np\n", - "\n", - "# Import the pandas package under the name pd\n", - "import pandas as pd\n", - "\n", - "# Import the matplotlib package under the name plt\n", - "import matplotlib.pyplot as plt\n", - "%matplotlib inline\n", - "\n", - "# Print the pandas version and the configuration\n", - "print(pd.__version__)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## DataFrame creation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Create an empty pandas DataFrame\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "pd.DataFrame(data=[None],\n", - " index=[None],\n", - " columns=[None])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a `marvel_df` pandas DataFrame with the given marvel data\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "marvel_data = [\n", - " ['Spider-Man', 'male', 1962],\n", - " ['Captain America', 'male', 1941],\n", - " ['Wolverine', 'male', 1974],\n", - " ['Iron Man', 'male', 1963],\n", - " ['Thor', 'male', 1963],\n", - " ['Thing', 'male', 1961],\n", - " ['Mister Fantastic', 'male', 1961],\n", - " ['Hulk', 'male', 1962],\n", - " ['Beast', 'male', 1963],\n", - " ['Invisible Woman', 'female', 1961],\n", - " ['Storm', 'female', 1975],\n", - " ['Namor', 'male', 1939],\n", - " ['Hawkeye', 'male', 1964],\n", - " ['Daredevil', 'male', 1964],\n", - " ['Doctor Strange', 'male', 1963],\n", - " ['Hank Pym', 'male', 1962],\n", - " ['Scarlet Witch', 'female', 1964],\n", - " ['Wasp', 'female', 1963],\n", - " ['Black Widow', 'female', 1964],\n", - " ['Vision', 'male', 1968]\n", - "]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "marvel_df = pd.DataFrame(data=marvel_data)\n", - "\n", - "marvel_df" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Add column names to the `marvel_df`\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "col_names = ['name', 'sex', 'first_appearance']\n", - "\n", - "marvel_df.columns = col_names\n", - "marvel_df" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Add index names to the `marvel_df` (use the character name as index)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "marvel_df.index = marvel_df['name']\n", - "marvel_df" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Drop the name column as it's now the index" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "#marvel_df = marvel_df.drop(columns=['name'])\n", - "marvel_df = marvel_df.drop(['name'], axis=1)\n", - "marvel_df" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Drop 'Namor' and 'Hank Pym' rows\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "marvel_df = marvel_df.drop(['Namor', 'Hank Pym'], axis=0)\n", - "marvel_df" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## DataFrame selection, slicing and indexation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Show the first 5 elements on `marvel_df`\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "#marvel_df.loc[['Spider-Man', 'Captain America', 'Wolverine', 'Iron Man', 'Thor'], :] # bad!\n", - "#marvel_df.loc['Spider-Man': 'Thor', :]\n", - "#marvel_df.iloc[0:5, :]\n", - "#marvel_df.iloc[0:5,]\n", - "marvel_df.iloc[:5,]\n", - "#marvel_df.head()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Show the last 5 elements on `marvel_df`\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "#marvel_df.loc[['Hank Pym', 'Scarlet Witch', 'Wasp', 'Black Widow', 'Vision'], :] # bad!\n", - "#marvel_df.loc['Hank Pym':'Vision', :]\n", - "marvel_df.iloc[-5:,]\n", - "#marvel_df.tail()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Show just the sex of the first 5 elements on `marvel_df`" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "#marvel_df.iloc[:5,]['sex'].to_frame()\n", - "marvel_df.iloc[:5,].sex.to_frame()\n", - "#marvel_df.head().sex.to_frame()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Show the first_appearance of all middle elements on `marvel_df` " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "marvel_df.iloc[1:-1,].first_appearance.to_frame()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Show the first and last elements on `marvel_df`\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "#marvel_df.iloc[[0, -1],][['sex', 'first_appearance']]\n", - "marvel_df.iloc[[0, -1],]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## DataFrame manipulation and operations" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Modify the `first_appearance` of 'Vision' to year 1964" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "marvel_df.loc['Vision', 'first_appearance'] = 1964\n", - "\n", - "marvel_df" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Add a new column to `marvel_df` called 'years_since' with the years since `first_appearance`\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "marvel_df['years_since'] = 2018 - marvel_df['first_appearance']\n", - "\n", - "marvel_df" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## DataFrame boolean arrays (also called masks)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Given the `marvel_df` pandas DataFrame, make a mask showing the female characters\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "mask = marvel_df['sex'] == 'female'\n", - "\n", - "mask" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the `marvel_df` pandas DataFrame, get the male characters\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "mask = marvel_df['sex'] == 'male'\n", - "\n", - "marvel_df[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the `marvel_df` pandas DataFrame, get the characters with `first_appearance` after 1970\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "mask = marvel_df['first_appearance'] > 1970\n", - "\n", - "marvel_df[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the `marvel_df` pandas DataFrame, get the female characters with `first_appearance` after 1970" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution", - "scrolled": true - }, - "outputs": [], - "source": [ - "mask = (marvel_df['sex'] == 'female') & (marvel_df['first_appearance'] > 1970)\n", - "\n", - "marvel_df[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## DataFrame summary statistics" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Show basic statistics of `marvel_df`" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "marvel_df.describe()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the `marvel_df` pandas DataFrame, show the mean value of `first_appearance`" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "\n", - "#np.mean(marvel_df.first_appearance)\n", - "marvel_df.first_appearance.mean()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the `marvel_df` pandas DataFrame, show the min value of `first_appearance`\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "#np.min(marvel_df.first_appearance)\n", - "marvel_df.first_appearance.min()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the `marvel_df` pandas DataFrame, get the characters with the min value of `first_appearance`" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "mask = marvel_df['first_appearance'] == marvel_df.first_appearance.min()\n", - "marvel_df[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## DataFrame basic plottings" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Reset index names of `marvel_df`\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "marvel_df = marvel_df.reset_index()\n", - "\n", - "marvel_df" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Plot the values of `first_appearance`\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "#plt.plot(marvel_df.index, marvel_df.first_appearance)\n", - "marvel_df.first_appearance.plot()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Plot a histogram (plot.hist) with values of `first_appearance`\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "\n", - "plt.hist(marvel_df.first_appearance)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.1" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + }, + "colab": { + "name": "4 - Pandas DataFrames exercises.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "DetNK0pQjLX_" + }, + "source": [ + "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", + "
\n", + "\n", + "# Pandas DataFrame exercises\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bKcHu6v5jLYI" + }, + "source": [ + "# Import the numpy package under the name np\n", + "import numpy as np\n", + "\n", + "# Import the pandas package under the name pd\n", + "import pandas as pd\n", + "\n", + "# Import the matplotlib package under the name plt\n", + "import matplotlib.pyplot as plt\n", + "%matplotlib inline\n", + "\n", + "# Print the pandas version and the configuration\n", + "print(pd.__version__)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qskaPzfejLYQ" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## DataFrame creation" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bWFGzQHCjLYS" + }, + "source": [ + "### Create an empty pandas DataFrame\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "jk2u9QChjLYS" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FbSbPTLvjVPL" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "_0y_r-YHjLYT" + }, + "source": [ + "pd.DataFrame(data=[None],\n", + " index=[None],\n", + " columns=[None])" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wYmJVu3cjLYU" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a `marvel_df` pandas DataFrame with the given marvel data\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "t9dXtIC0jLYT" + }, + "source": [ + "" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kd8gTldbjLYU" + }, + "source": [ + "marvel_data = [\n", + " ['Spider-Man', 'male', 1962],\n", + " ['Captain America', 'male', 1941],\n", + " ['Wolverine', 'male', 1974],\n", + " ['Iron Man', 'male', 1963],\n", + " ['Thor', 'male', 1963],\n", + " ['Thing', 'male', 1961],\n", + " ['Mister Fantastic', 'male', 1961],\n", + " ['Hulk', 'male', 1962],\n", + " ['Beast', 'male', 1963],\n", + " ['Invisible Woman', 'female', 1961],\n", + " ['Storm', 'female', 1975],\n", + " ['Namor', 'male', 1939],\n", + " ['Hawkeye', 'male', 1964],\n", + " ['Daredevil', 'male', 1964],\n", + " ['Doctor Strange', 'male', 1963],\n", + " ['Hank Pym', 'male', 1962],\n", + " ['Scarlet Witch', 'female', 1964],\n", + " ['Wasp', 'female', 1963],\n", + " ['Black Widow', 'female', 1964],\n", + " ['Vision', 'male', 1968]\n", + "]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "VXh3cqtHjLYV" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6l2ZKRDijaqs" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "cH4wD_X3jLYW" + }, + "source": [ + "marvel_df = pd.DataFrame(data=marvel_data)\n", + "\n", + "marvel_df" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iiuxrOppjLYX" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Add column names to the `marvel_df`\n", + " " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XhCkhHDTjLYX" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GIFutRikjbkP" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "iVIa9fjJjLYY" + }, + "source": [ + "col_names = ['name', 'sex', 'first_appearance']\n", + "\n", + "marvel_df.columns = col_names\n", + "marvel_df" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WY8XWTWzjLYe" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Add index names to the `marvel_df` (use the character name as index)\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xrjBjwJbjLYg" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kIAW640PjcNL" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "dYGQaJsJjLYh" + }, + "source": [ + "marvel_df.index = marvel_df['name']\n", + "marvel_df" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HPrLJ36yjLYh" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Drop the name column as it's now the index" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "QqBoWZnAjLYj" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iNcstNC3jird" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "yvYUKNxCjLYj" + }, + "source": [ + "#marvel_df = marvel_df.drop(columns=['name'])\n", + "marvel_df = marvel_df.drop(['name'], axis=1)\n", + "marvel_df" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LJRIvQrVjLYk" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Drop 'Namor' and 'Hank Pym' rows\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "fcsadtzwjLYk" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rd1AZI7yjggi" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "jsMNzHGQjLYk" + }, + "source": [ + "marvel_df = marvel_df.drop(['Namor', 'Hank Pym'], axis=0)\n", + "marvel_df" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uidbW-YCjLYo" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## DataFrame selection, slicing and indexation" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7fDmCKzhjLYp" + }, + "source": [ + "### Show the first 5 elements on `marvel_df`\n", + " " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Og71qXfmjLYp" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "k0C6ZQNnj6cA" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "nxqWXkcRjLYt" + }, + "source": [ + "#marvel_df.loc[['Spider-Man', 'Captain America', 'Wolverine', 'Iron Man', 'Thor'], :] # bad!\n", + "#marvel_df.loc['Spider-Man': 'Thor', :]\n", + "#marvel_df.iloc[0:5, :]\n", + "#marvel_df.iloc[0:5,]\n", + "marvel_df.iloc[:5,]\n", + "#marvel_df.head()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "26C_Qb_MjLYx" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Show the last 5 elements on `marvel_df`\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "80x4Jq95jLYx" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nOP_LBWxj7Cg" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "ekGHxWxwjLYy" + }, + "source": [ + "#marvel_df.loc[['Hank Pym', 'Scarlet Witch', 'Wasp', 'Black Widow', 'Vision'], :] # bad!\n", + "#marvel_df.loc['Hank Pym':'Vision', :]\n", + "marvel_df.iloc[-5:,]\n", + "#marvel_df.tail()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yBiWXOy1jLYz" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Show just the sex of the first 5 elements on `marvel_df`" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cXbqIbV7jLYz" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "f7g8CyoPj71s" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "uCE4-T-CjLYz" + }, + "source": [ + "#marvel_df.iloc[:5,]['sex'].to_frame()\n", + "marvel_df.iloc[:5,].sex.to_frame()\n", + "#marvel_df.head().sex.to_frame()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cPUUIybBjLY0" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Show the first_appearance of all middle elements on `marvel_df` " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xN-9P4ODjLY0" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "R5Sa9WV6j8m7" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "NYvzBUPojLY0" + }, + "source": [ + "marvel_df.iloc[1:-1,].first_appearance.to_frame()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gAUqL_MhjLY0" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Show the first and last elements on `marvel_df`\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4uH4rj6djLY1" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J1tzMDVMj9QD" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "ZrTZTBXIjLY1" + }, + "source": [ + "#marvel_df.iloc[[0, -1],][['sex', 'first_appearance']]\n", + "marvel_df.iloc[[0, -1],]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FgsOUKIijLY1" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## DataFrame manipulation and operations" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zFFnIckAjLY2" + }, + "source": [ + "### Modify the `first_appearance` of 'Vision' to year 1964" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PPEHONL0jLY2" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZW7m9G0Mj90R" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "OZWuPfXtjLY2" + }, + "source": [ + "marvel_df.loc['Vision', 'first_appearance'] = 1964\n", + "\n", + "marvel_df" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bgLi1h50jLY3" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Add a new column to `marvel_df` called 'years_since' with the years since `first_appearance`\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "0IVAdKqdjLY3" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BsRqXZIWj-l_" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "hsx7RanLjLY3" + }, + "source": [ + "marvel_df['years_since'] = 2018 - marvel_df['first_appearance']\n", + "\n", + "marvel_df" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pHuEcc9SjLY3" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## DataFrame boolean arrays (also called masks)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KXPIwRKLjLY4" + }, + "source": [ + "### Given the `marvel_df` pandas DataFrame, make a mask showing the female characters\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1Im0LUdWjLY4" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2liXysTYkJbp" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "8rhBQ6sGjLY4" + }, + "source": [ + "mask = marvel_df['sex'] == 'female'\n", + "\n", + "mask" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gZOssIPtjLY4" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the `marvel_df` pandas DataFrame, get the male characters\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SBJsz-ocjLY4" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Os0C1v7GkKNL" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "qFmzb3xhjLY6" + }, + "source": [ + "mask = marvel_df['sex'] == 'male'\n", + "\n", + "marvel_df[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1yZ85OQ1jLZC" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the `marvel_df` pandas DataFrame, get the characters with `first_appearance` after 1970\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3STDMvbLjLZC" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JAzaLfqYkKv8" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "KdlEOkZLjLZC" + }, + "source": [ + "mask = marvel_df['first_appearance'] > 1970\n", + "\n", + "marvel_df[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6KYhmXNUjLZD" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the `marvel_df` pandas DataFrame, get the female characters with `first_appearance` after 1970" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "CaAKusZLjLZD" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2la6t8LrkLWq" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "scrolled": true, + "id": "CrrasUNljLZD" + }, + "source": [ + "mask = (marvel_df['sex'] == 'female') & (marvel_df['first_appearance'] > 1970)\n", + "\n", + "marvel_df[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "43zJ5PU2jLZD" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## DataFrame summary statistics" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Rfe9Q_fDjLZD" + }, + "source": [ + "### Show basic statistics of `marvel_df`" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A_8rxatFjLZE" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kE3LM7TAkQ5W" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "qxDPgyjSjLZE" + }, + "source": [ + "marvel_df.describe()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NW3FB_NgjLZE" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the `marvel_df` pandas DataFrame, show the mean value of `first_appearance`" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "BIls1kUJjLZE" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uocq7tdVkRlG" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "X_DRrH7ljLZE" + }, + "source": [ + "\n", + "#np.mean(marvel_df.first_appearance)\n", + "marvel_df.first_appearance.mean()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vuhRS12EjLZF" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the `marvel_df` pandas DataFrame, show the min value of `first_appearance`\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "viOWed-AjLZF" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EutiFPPpkSRa" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "4kSSaJijjLZF" + }, + "source": [ + "#np.min(marvel_df.first_appearance)\n", + "marvel_df.first_appearance.min()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r-3bsrsHjLZF" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the `marvel_df` pandas DataFrame, get the characters with the min value of `first_appearance`" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "G1MUq1VGjLZG" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3QDGWHEfkS3d" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "LXtprMqijLZG" + }, + "source": [ + "mask = marvel_df['first_appearance'] == marvel_df.first_appearance.min()\n", + "marvel_df[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nTj4KLpMjLZH" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## DataFrame basic plottings" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IgHDLgpejLZH" + }, + "source": [ + "### Reset index names of `marvel_df`\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "31KAfhQPjLZH" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "s8VxHKoekZub" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "-cjJwiJsjLZH" + }, + "source": [ + "marvel_df = marvel_df.reset_index()\n", + "\n", + "marvel_df" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cH_iEwvijLZI" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Plot the values of `first_appearance`\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kRjTn8PSjLZI" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "e0LPAad8kakr" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "vHhSIsrcjLZI" + }, + "source": [ + "#plt.plot(marvel_df.index, marvel_df.first_appearance)\n", + "marvel_df.first_appearance.plot()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xFNkvfqIjLZI" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Plot a histogram (plot.hist) with values of `first_appearance`\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "_4VRp1x_jLZI" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3MFce_CEkbPS" + }, + "source": [ + "#### _Answer:_" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "L0TsOCCljLZJ" + }, + "source": [ + "plt.hist(marvel_df.first_appearance)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0v3zP09cjLZJ" + }, + "source": [ + "## ![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n" + ] + } + ] +} \ No newline at end of file