diff --git a/.gitignore b/.gitignore index 6c4cf0f..c5d1999 100644 --- a/.gitignore +++ b/.gitignore @@ -74,7 +74,7 @@ docs/_build/ target/ # Jupyter Notebook -.ipynb_checkpoints +*.ipynb_checkpoints/ # IPython profile_default/ diff --git a/Py_for_DS_and_ML_Bootcamp/1_Python Crash Course/01-Python Crash Course.ipynb b/Py_for_DS_and_ML_Bootcamp/1_Python Crash Course/01-Python Crash Course.ipynb new file mode 100644 index 0000000..f277b73 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/1_Python Crash Course/01-Python Crash Course.ipynb @@ -0,0 +1,2042 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "# Python Crash Course\n", + "_____\n", + "This notebook will just go through the basic topics in order:\n", + "\n", + "* Data types\n", + " * Numbers\n", + " * Strings\n", + " * Printing\n", + " * Lists\n", + " * Dictionaries\n", + " * Booleans\n", + " * Tuples \n", + " * Sets\n", + "* Comparison Operators\n", + "* if, elif, else Statements\n", + "* for Loops\n", + "* while Loops\n", + "* range()\n", + "* list comprehension\n", + "* functions\n", + "* lambda expressions\n", + "* map and filter\n", + "* methods\n", + "____" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Data types\n", + "\n", + "### Numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 + 1" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 * 3" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.5" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 / 2" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "16" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 ** 4" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "4 % 2" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5 % 2" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "50" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(2 + 3) * (5 + 5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Variable Assignment" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "# Can not start with number or special characters\n", + "name_of_var = 2" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "x = 2\n", + "y = 3" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "z = x + y" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "z" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Strings" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'single quotes'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'single quotes'" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'double quotes'" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"double quotes\"" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\" wrap lot's of other quotes\"" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\" wrap lot's of other quotes\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Printing" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "x = 'hello'" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello\n" + ] + } + ], + "source": [ + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "num = 12\n", + "name = 'Ben'" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My number is: 12, and my name is: Ben\n" + ] + } + ], + "source": [ + "print('My number is: {one}, and my name is: {two}'.format(one=num,two=name))" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My number is: 12, and my name is: Ben\n" + ] + } + ], + "source": [ + "print('My number is: {}, and my name is: {}'.format(num,name))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Lists" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3]" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[1,2,3]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['hi', 1, [1, 2]]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "['hi',1,[1,2]]" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "my_list = ['a','b','c']" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "my_list.append('d')" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'b', 'c', 'd']" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'a'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'b'" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['b', 'c', 'd']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list[1:]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list[:1]" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "my_list[0] = 'New_Value'" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['New_Value', 'b', 'c', 'd']" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [], + "source": [ + "nest = [1,2,3,[4,5,['target']]]" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 5, ['target']]" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nest[3]" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['target']" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nest[3][2]" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'target'" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nest[3][2][0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Dictionaries" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "d = {'key1':'item1','key2':'item2'}" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'key1': 'item1', 'key2': 'item2'}" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'item1'" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d['key1']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Booleans" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "True" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tuples " + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "t = (1,2,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'NEW'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t[0] = 'NEW'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Sets" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3}" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{1,2,3}" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3}" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{1,2,3,1,2,1,2,3,3,3,3,2,2,2,1,1,2}" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{0, 3, 4, 5, 6, 7, 8, 9}" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set([5,6,7,7,7,8,9,0,0,6,5,4,4,4,4,3,3,3])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Comparison Operators" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 > 2" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 < 2" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 >= 1" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 <= 4" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 == 1" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'hi' == 'bye'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Logic Operators" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(1 > 2) and (2 < 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(1 > 2) or (2 < 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(1 == 2) or (2 == 3) or (4 == 4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## if,elif, else Statements" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Yep!\n" + ] + } + ], + "source": [ + "if 1 < 2:\n", + " print('Yep!')" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "yep!\n" + ] + } + ], + "source": [ + "if 1 < 2:\n", + " print('yep!')" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "first\n" + ] + } + ], + "source": [ + "if 1 < 2:\n", + " print('first')\n", + "else:\n", + " print('last')" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "last\n" + ] + } + ], + "source": [ + "if 1 > 2:\n", + " print('first')\n", + "else:\n", + " print('last')" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "middle\n" + ] + } + ], + "source": [ + "if 1 == 2:\n", + " print('first')\n", + "elif 3 == 3:\n", + " print('middle')\n", + "else:\n", + " print('Last')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## for Loops" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [], + "source": [ + "seq = [1,2,3,4,5]" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "for item in seq:\n", + " print(item)" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Yep\n", + "Yep\n", + "Yep\n", + "Yep\n", + "Yep\n" + ] + } + ], + "source": [ + "for item in seq:\n", + " print('Yep')" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "4\n", + "6\n", + "8\n", + "10\n" + ] + } + ], + "source": [ + "for jelly in seq:\n", + " print(jelly+jelly)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## while Loops" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "i is: 1\n", + "i is: 2\n", + "i is: 3\n", + "i is: 4\n" + ] + } + ], + "source": [ + "i = 1\n", + "while i < 5:\n", + " print('i is: {}'.format(i))\n", + " i = i+1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## range()" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "range(0, 5)" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "range(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "for i in range(5):\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4]" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(range(5))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## list comprehension" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [], + "source": [ + "x = [1,2,3,4]" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16]\n" + ] + } + ], + "source": [ + "out = []\n", + "for item in x:\n", + " out.append(item**2)\n", + "print(out)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16]" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[item**2 for item in x]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## functions" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [], + "source": [ + "def my_func(param1='default'):\n", + " \"\"\"\n", + " Docstring goes here.\n", + " \"\"\"\n", + " print(param1)" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_func" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "default\n" + ] + } + ], + "source": [ + "my_func()" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "new param\n" + ] + } + ], + "source": [ + "my_func('new param')" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "new param\n" + ] + } + ], + "source": [ + "my_func(param1='new param')" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [], + "source": [ + "def square(x):\n", + " return x**2" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [], + "source": [ + "out = square(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "print(out)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## lambda expressions" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [], + "source": [ + "def times2(var):\n", + " return var*2" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "times2(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + ">" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lambda var: var*2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## map and filter" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "seq = [1,2,3,4,5]" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "map(times2,seq)" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 4, 6, 8, 10]" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(times2,seq))" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 4, 6, 8, 10]" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(lambda var: var*2,seq))" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "filter(lambda item: item%2 == 0,seq)" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 4]" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(filter(lambda item: item%2 == 0,seq))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## methods" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "st = 'hello my name is Sam'" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello my name is sam'" + ] + }, + "execution_count": 112, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "st.lower()" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'HELLO MY NAME IS SAM'" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "st.upper()" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['hello', 'my', 'name', 'is', 'Sam']" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "st.split()" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "tweet = 'Go Sports! #Sports'" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Go Sports! ', 'Sports']" + ] + }, + "execution_count": 106, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tweet.split('#')" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Sports'" + ] + }, + "execution_count": 107, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tweet.split('#')[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'key1': 'item1', 'key2': 'item2'}" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['key2', 'key1'])" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('key2', 'item2'), ('key1', 'item1')])" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.items()" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "lst = [1,2,3]" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2]" + ] + }, + "execution_count": 108, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 109, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'x' in [1,2,3]" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 110, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'x' in ['x','y','z']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/1_Python Crash Course/02-Python Crash Course Exercises.ipynb b/Py_for_DS_and_ML_Bootcamp/1_Python Crash Course/02-Python Crash Course Exercises.ipynb new file mode 100644 index 0000000..0e4be5e --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/1_Python Crash Course/02-Python Crash Course Exercises.ipynb @@ -0,0 +1,501 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "# Python Crash Course Exercises \n", + "\n", + "This is test your understanding of Python Basics." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercises\n", + "\n", + "Answer the questions or complete the tasks outlined in bold below, use the specific method described if applicable." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** What is 7 to the power of 4?**" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2401" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "7 ** 4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Split this string:**\n", + "\n", + " s = \"Hi there Sam!\"\n", + " \n", + "**into a list. **" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Hi', 'there', 'Sam!']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = \"Hi there Sam!\"\n", + "s.split()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Given the variables:**\n", + "\n", + " planet = \"Earth\"\n", + " diameter = 12742\n", + "\n", + "** Use .format() to print the following string: **\n", + "\n", + " The diameter of Earth is 12742 kilometers." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "planet = \"Earth\"\n", + "diameter = 12742" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The diameter of Earth is 12742 kilometers.\n" + ] + } + ], + "source": [ + "print(\"The diameter of {} is {} kilometers.\".format(planet,diameter))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Given this nested list, use indexing to grab the word \"hello\" **" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['hello']" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst[3][1][2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Given this nested dictionary grab the word \"hello\". Be prepared, this will be annoying/tricky **" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello'" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d['k1'][3]['tricky'][3]['target'][3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** What is the main difference between a tuple and a list? **" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Tuple is immutable" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Create a function that grabs the email website domain from a string in the form: **\n", + "\n", + " user@domain.com\n", + " \n", + "**So for example, passing \"user@domain.com\" would return: domain.com**" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "def domainGet(email):\n", + " domain = email.split('@')[1]\n", + " return domain" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'domain.com'" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "domainGet('user@domain.com')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Create a basic function that returns True if the word 'dog' is contained in the input string. Don't worry about edge cases like a punctuation being attached to the word dog, but do account for capitalization. **" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "def findDog(string):\n", + " if 'dog' in string:\n", + " return True\n", + " else:\n", + " return \"No dog found\"" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "findDog('Is there a dog here?')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Create a function that counts the number of times the word \"dog\" occurs in a string. Again ignore edge cases. **" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "def countDog(text):\n", + " count = 0\n", + " words = text.split()\n", + " for word in words:\n", + " if word == 'dog':\n", + " count += 1\n", + " return count" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "countDog('This dog runs faster than the other dog dude!')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def countDog(st):\n", + " count = 0\n", + " for word in st.lower().split():\n", + " if word == 'dog':\n", + " count += 1\n", + " return count" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:**\n", + "\n", + " seq = ['soup','dog','salad','cat','great']\n", + "\n", + "**should be filtered down to:**\n", + "\n", + " ['soup','salad']" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "seq = ['soup','dog','salad','cat','great']" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['soup', 'salad']" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(filter(lambda item: item[0] == 's' ,seq))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Final Problem\n", + "**You are driving a little too fast, and a police officer stops you. Write a function\n", + " to return one of 3 possible results: \"No ticket\", \"Small ticket\", or \"Big Ticket\". \n", + " If your speed is 60 or less, the result is \"No Ticket\". If speed is between 61 \n", + " and 80 inclusive, the result is \"Small Ticket\". If speed is 81 or more, the result is \"Big Ticket\". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all \n", + " cases. **" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": {}, + "outputs": [], + "source": [ + "def caught_speeding(speed, is_birthday):\n", + " ticket_1 = \"No ticket\"\n", + " ticket_2 = \"Small ticket\"\n", + " ticket_3 = \"Big ticket\"\n", + " if is_birthday == True: \n", + " speed = speed - 5\n", + " \n", + " if speed > 80:\n", + " return ticket_3\n", + " elif speed < 80 and speed >60 :\n", + " return ticket_2\n", + " else: \n", + " return ticket_1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Small ticket'" + ] + }, + "execution_count": 124, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "caught_speeding(81,True)" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Big ticket'" + ] + }, + "execution_count": 125, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "caught_speeding(81,False)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great job!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/1_Python Crash Course/README.md b/Py_for_DS_and_ML_Bootcamp/1_Python Crash Course/README.md new file mode 100644 index 0000000..63ad0b9 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/1_Python Crash Course/README.md @@ -0,0 +1,24 @@ +This is an exercise for understanding the basics of Python. + +This is a walkthrough on the basic topics in order: + +* Data types + * Numbers + * Strings + * Printing + * Lists + * Dictionaries + * Booleans + * Tuples + * Sets +* Comparison Operators +* if, elif, else Statements +* for Loops +* while Loops +* range() +* list comprehension +* functions +* lambda expressions +* map and filter +* methods +____ diff --git a/Py_for_DS_and_ML_Bootcamp/2_NumPy/01-NumPy Arrays.ipynb b/Py_for_DS_and_ML_Bootcamp/2_NumPy/01-NumPy Arrays.ipynb new file mode 100644 index 0000000..bb435c8 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/2_NumPy/01-NumPy Arrays.ipynb @@ -0,0 +1,916 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# NumPy \n", + "\n", + "NumPy (or Numpy) is a Linear Algebra Library for Python, the reason it is so important for Data Science with Python is that almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks.\n", + "\n", + "Numpy is also incredibly fast, as it has bindings to C libraries. For more info on why you would want to use Arrays instead of lists, check out this great [StackOverflow post](http://stackoverflow.com/questions/993984/why-numpy-instead-of-python-lists)." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Creating NumPy Arrays\n", + "\n", + "### From a Python List\n", + "\n", + "We can create an array by directly converting a list or list of lists:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list = [1,2,3]\n", + "my_list" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3])" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = np.array(my_list)\n", + "\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_matrix = [[1,2,3],[4,5,6],[7,8,9]]\n", + "my_matrix" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]])" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.array(my_matrix)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Built-in Methods\n", + "\n", + "There are lots of built-in ways to generate Arrays" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### arange\n", + "\n", + "Return evenly spaced values within a given interval." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(0,10)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0, 2, 4, 6, 8, 10])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(0,11,2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### zeros and ones\n", + "\n", + "Generate arrays of zeros or ones" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0., 0., 0.])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.zeros(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0.]])" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.zeros((5,5))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1., 1., 1.])" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1., 1., 1.],\n", + " [1., 1., 1.],\n", + " [1., 1., 1.]])" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones((3,3))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### linspace\n", + "Return evenly spaced numbers over a specified interval." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0., 5., 10.])" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.linspace(0,10,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653,\n", + " 1.02040816, 1.2244898 , 1.42857143, 1.63265306, 1.83673469,\n", + " 2.04081633, 2.24489796, 2.44897959, 2.65306122, 2.85714286,\n", + " 3.06122449, 3.26530612, 3.46938776, 3.67346939, 3.87755102,\n", + " 4.08163265, 4.28571429, 4.48979592, 4.69387755, 4.89795918,\n", + " 5.10204082, 5.30612245, 5.51020408, 5.71428571, 5.91836735,\n", + " 6.12244898, 6.32653061, 6.53061224, 6.73469388, 6.93877551,\n", + " 7.14285714, 7.34693878, 7.55102041, 7.75510204, 7.95918367,\n", + " 8.16326531, 8.36734694, 8.57142857, 8.7755102 , 8.97959184,\n", + " 9.18367347, 9.3877551 , 9.59183673, 9.79591837, 10. ])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.linspace(0,10,50)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## eye\n", + "\n", + "Creates an identity matrix" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1., 0., 0., 0.],\n", + " [0., 1., 0., 0.],\n", + " [0., 0., 1., 0.],\n", + " [0., 0., 0., 1.]])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.eye(4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Random \n", + "\n", + "Numpy also has lots of ways to create random number arrays:\n", + "\n", + "### rand\n", + "Create an array of the given shape and populate it with\n", + "random samples from a uniform distribution\n", + "over ``[0, 1)``." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0.50414296, 0.49332045])" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.rand(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0.32360792, 0.98114791, 0.28602552, 0.08416343, 0.41674674],\n", + " [0.10451488, 0.50380413, 0.51340603, 0.91389559, 0.01348767],\n", + " [0.40418579, 0.32071623, 0.93895126, 0.04504914, 0.42281561],\n", + " [0.65220508, 0.54184117, 0.97947881, 0.02241656, 0.80589127],\n", + " [0.64372145, 0.0619712 , 0.19777373, 0.49030804, 0.35037883]])" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.rand(5,5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### randn\n", + "\n", + "Return a sample (or samples) from the \"standard normal\" distribution. Unlike rand which is uniform:" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1.0255307 , 0.42727577])" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.randn(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0.28961379, -1.14917061, -2.01445256, 0.652331 , -0.33978562],\n", + " [ 2.12360233, -0.27918853, 0.72281839, -0.19027032, -0.9048893 ],\n", + " [-0.71786056, -0.60827758, -1.35111618, -0.43191901, 2.63733253],\n", + " [-0.58903396, 1.0653403 , 0.28796622, -0.1464022 , -2.7838868 ],\n", + " [-1.01424219, 0.4590674 , 0.1190054 , 0.45138727, -0.67789957]])" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.randn(5,5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### randint\n", + "Return random integers from `low` (inclusive) to `high` (exclusive)." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "44" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.randint(1,100)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([61, 88, 15, 78, 28, 16, 21, 44, 20, 20])" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.randint(1,100,10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Array Attributes and Methods\n", + "\n", + "Let's discuss some useful attributes and methods or an array:" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "arr = np.arange(25)\n", + "ranarr = np.random.randint(0,50,10)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", + " 17, 18, 19, 20, 21, 22, 23, 24])" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([31, 35, 1, 48, 41, 5, 32, 22, 35, 0])" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ranarr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reshape\n", + "Returns an array containing the same data with a new shape." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0, 1, 2, 3, 4],\n", + " [ 5, 6, 7, 8, 9],\n", + " [10, 11, 12, 13, 14],\n", + " [15, 16, 17, 18, 19],\n", + " [20, 21, 22, 23, 24]])" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr.reshape(5,5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### max,min,argmax,argmin\n", + "\n", + "These are useful methods for finding max or min values. Or to find their index locations using argmin or argmax" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([31, 35, 1, 48, 41, 5, 32, 22, 35, 0])" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ranarr" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "48" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ranarr.max()" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ranarr.argmax()" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ranarr.min()" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ranarr.argmin()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Shape\n", + "\n", + "Shape is an attribute that arrays have (not a method):" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(25,)" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Vector\n", + "arr.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,\n", + " 16, 17, 18, 19, 20, 21, 22, 23, 24]])" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Notice the two sets of brackets\n", + "arr.reshape(1,25)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 25)" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr.reshape(1,25).shape" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0],\n", + " [ 1],\n", + " [ 2],\n", + " [ 3],\n", + " [ 4],\n", + " [ 5],\n", + " [ 6],\n", + " [ 7],\n", + " [ 8],\n", + " [ 9],\n", + " [10],\n", + " [11],\n", + " [12],\n", + " [13],\n", + " [14],\n", + " [15],\n", + " [16],\n", + " [17],\n", + " [18],\n", + " [19],\n", + " [20],\n", + " [21],\n", + " [22],\n", + " [23],\n", + " [24]])" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr.reshape(25,1)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(25, 1)" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr.reshape(25,1).shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### dtype\n", + "\n", + "You can also grab the data type of the object in the array:" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dtype('int64')" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr.dtype" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/2_NumPy/02-Numpy Indexing and Selection.ipynb b/Py_for_DS_and_ML_Bootcamp/2_NumPy/02-Numpy Indexing and Selection.ipynb new file mode 100644 index 0000000..ff5f525 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/2_NumPy/02-Numpy Indexing and Selection.ipynb @@ -0,0 +1,750 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# NumPy Indexing and Selection" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "#Creating sample array\n", + "arr = np.arange(0,11)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Show\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Bracket Indexing and Selection\n", + "The simplest way to pick one or some elements of an array looks very similar to python lists:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Get a value at an index\n", + "arr[8]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3, 4])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Get values in a range\n", + "arr[1:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0, 1, 2, 3, 4])" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Get values in a range\n", + "arr[0:5]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Broadcasting\n", + "\n", + "Numpy arrays differ from a normal Python list because of their ability to broadcast:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([100, 100, 100, 100, 100, 5, 6, 7, 8, 9, 10])" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Setting a value with index range (Broadcasting)\n", + "arr[0:5]=100\n", + "\n", + "#Show\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Reset array, we'll see why I had to reset in a moment\n", + "arr = np.arange(0,11)\n", + "\n", + "#Show\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0, 1, 2, 3, 4, 5])" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Important notes on Slices\n", + "slice_of_arr = arr[0:6]\n", + "\n", + "#Show slice\n", + "slice_of_arr" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([99, 99, 99, 99, 99, 99])" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Change Slice\n", + "slice_of_arr[:]=99\n", + "\n", + "#Show Slice again\n", + "slice_of_arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now note the changes also occur in our original array!" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Data is not copied, it's a view of the original array! This avoids memory problems!" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#To get a copy, need to be explicit\n", + "arr_copy = arr.copy()\n", + "\n", + "arr_copy" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Indexing a 2D array (matrices)\n", + "\n", + "The general format is **arr_2d[row][col]** or **arr_2d[row,col]**. I recommend usually using the comma notation for clarity." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 5, 10, 15],\n", + " [20, 25, 30],\n", + " [35, 40, 45]])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))\n", + "\n", + "#Show\n", + "arr_2d" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([20, 25, 30])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Indexing row\n", + "arr_2d[1]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Format is arr_2d[row][col] or arr_2d[row,col]\n", + "\n", + "# Getting individual element value\n", + "arr_2d[1][0]" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Getting individual element value\n", + "arr_2d[1,0]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[10, 15],\n", + " [25, 30]])" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 2D array slicing\n", + "\n", + "#Shape (2,2) from top right corner\n", + "arr_2d[:2,1:]" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([35, 40, 45])" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Shape bottom row\n", + "arr_2d[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([35, 40, 45])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Shape bottom row\n", + "arr_2d[2,:]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Fancy Indexing\n", + "\n", + "Fancy indexing allows you to select entire rows or columns out of order,to show this, let's quickly build out a numpy array:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "#Set up matrix\n", + "arr2d = np.zeros((10,10))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "#Length of array\n", + "arr_length = arr2d.shape[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", + " [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n", + " [2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],\n", + " [3., 3., 3., 3., 3., 3., 3., 3., 3., 3.],\n", + " [4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],\n", + " [5., 5., 5., 5., 5., 5., 5., 5., 5., 5.],\n", + " [6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],\n", + " [7., 7., 7., 7., 7., 7., 7., 7., 7., 7.],\n", + " [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],\n", + " [9., 9., 9., 9., 9., 9., 9., 9., 9., 9.]])" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Set up array\n", + "\n", + "for i in range(arr_length):\n", + " arr2d[i] = i\n", + " \n", + "arr2d" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Fancy indexing allows the following" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],\n", + " [4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],\n", + " [6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],\n", + " [8., 8., 8., 8., 8., 8., 8., 8., 8., 8.]])" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr2d[[2,4,6,8]]" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],\n", + " [4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],\n", + " [2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],\n", + " [7., 7., 7., 7., 7., 7., 7., 7., 7., 7.]])" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Allows in any order\n", + "arr2d[[6,4,2,7]]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## More Indexing Help" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Selection\n", + "\n", + "Let's briefly go over how to use brackets for selection based off of comparison operators." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = np.arange(1,11)\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([False, False, False, False, True, True, True, True, True,\n", + " True])" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr > 4" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "bool_arr = arr>4" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([False, False, False, False, True, True, True, True, True,\n", + " True])" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bool_arr" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 5, 6, 7, 8, 9, 10])" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr[bool_arr]" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 3, 4, 5, 6, 7, 8, 9, 10])" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr[arr>2]" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 3, 4, 5, 6, 7, 8, 9, 10])" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = 2\n", + "arr[arr>x]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/2_NumPy/03-Numpy Operations.ipynb b/Py_for_DS_and_ML_Bootcamp/2_NumPy/03-Numpy Operations.ipynb new file mode 100644 index 0000000..3bb067d --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/2_NumPy/03-Numpy Operations.ipynb @@ -0,0 +1,329 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# NumPy Operations" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Arithmetic\n", + "\n", + "You can easily perform array with array arithmetic, or scalar with array arithmetic. Let's see some examples:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "arr = np.arange(0,10)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr + arr" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr * arr" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr - arr" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in true_divide\n", + " if __name__ == '__main__':\n" + ] + }, + { + "data": { + "text/plain": [ + "array([ nan, 1., 1., 1., 1., 1., 1., 1., 1., 1.])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Warning on division by zero, but not an error!\n", + "# Just replaced with nan\n", + "arr/arr" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in true_divide\n", + " if __name__ == '__main__':\n" + ] + }, + { + "data": { + "text/plain": [ + "array([ inf, 1. , 0.5 , 0.33333333, 0.25 ,\n", + " 0.2 , 0.16666667, 0.14285714, 0.125 , 0.11111111])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Also warning, but not an error instead infinity\n", + "1/arr" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr**3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Universal Array Functions\n", + "\n", + "Numpy comes with many [universal array functions](http://docs.scipy.org/doc/numpy/reference/ufuncs.html), which are essentially just mathematical operations you can use to perform the operation across the array. Let's show some common ones:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0. , 1. , 1.41421356, 1.73205081, 2. ,\n", + " 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Taking Square Roots\n", + "np.sqrt(arr)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1.00000000e+00, 2.71828183e+00, 7.38905610e+00,\n", + " 2.00855369e+01, 5.45981500e+01, 1.48413159e+02,\n", + " 4.03428793e+02, 1.09663316e+03, 2.98095799e+03,\n", + " 8.10308393e+03])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Calcualting exponential (e^)\n", + "np.exp(arr)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.max(arr) #same as arr.max()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0. , 0.84147098, 0.90929743, 0.14112001, -0.7568025 ,\n", + " -0.95892427, -0.2794155 , 0.6569866 , 0.98935825, 0.41211849])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.sin(arr)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/marci/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in log\n", + " if __name__ == '__main__':\n" + ] + }, + { + "data": { + "text/plain": [ + "array([ -inf, 0. , 0.69314718, 1.09861229, 1.38629436,\n", + " 1.60943791, 1.79175947, 1.94591015, 2.07944154, 2.19722458])" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.log(arr)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!\n", + "\n", + "That's all we need to know for now!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/2_NumPy/04-Numpy Exercises.ipynb b/Py_for_DS_and_ML_Bootcamp/2_NumPy/04-Numpy Exercises.ipynb new file mode 100644 index 0000000..9bc847c --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/2_NumPy/04-Numpy Exercises.ipynb @@ -0,0 +1,666 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# NumPy Exercises \n", + "\n", + "Now that we've learned about NumPy let's test your knowledge. We'll start off with a few simple tasks, and then you'll be asked some more complicated questions." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Import NumPy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create an array of 10 zeros \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.zeros(10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create an array of 10 ones\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones(10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create an array of 10 fives" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([5., 5., 5., 5., 5., 5., 5., 5., 5., 5.])" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones(10)*5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create an array of the integers from 10 to 50" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n", + " 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,\n", + " 44, 45, 46, 47, 48, 49, 50])" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(10,51,1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create an array of all the even integers from 10 to 50" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,\n", + " 44, 46, 48, 50])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(10,51,2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create a 3x3 matrix with values ranging from 0 to 8" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0, 1, 2],\n", + " [3, 4, 5],\n", + " [6, 7, 8]])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(0,9).reshape(3,3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create a 3x3 identity matrix" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1., 0., 0.],\n", + " [0., 1., 0.],\n", + " [0., 0., 1.]])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.eye(3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Use NumPy to generate a random number between 0 and 1" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0.28212686])" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.rand(1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Use NumPy to generate an array of 25 random numbers sampled from a standard normal distribution" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0.28072455, 0.22200169, 0.10514183, 0.35016863, 0.22734648],\n", + " [0.04549791, 0.0735919 , 0.84459123, 0.12407278, 0.98842242],\n", + " [0.29471071, 0.46301896, 0.62365023, 0.87614584, 0.42385614],\n", + " [0.15547687, 0.08279872, 0.93448278, 0.37936524, 0.81830851],\n", + " [0.65006015, 0.16724568, 0.50507925, 0.85445288, 0.9098362 ]])" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.rand(5,5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create the following matrix:" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ,\n", + " 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 , 0.21,\n", + " 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 , 0.31, 0.32,\n", + " 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 , 0.41, 0.42, 0.43,\n", + " 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 , 0.51, 0.52, 0.53, 0.54,\n", + " 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 , 0.61, 0.62, 0.63, 0.64, 0.65,\n", + " 0.66, 0.67, 0.68, 0.69, 0.7 , 0.71, 0.72, 0.73, 0.74, 0.75, 0.76,\n", + " 0.77, 0.78, 0.79, 0.8 , 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87,\n", + " 0.88, 0.89, 0.9 , 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98,\n", + " 0.99])" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(0,1.,0.01)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create an array of 20 linearly spaced points between 0 and 1:" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0. , 0.05263158, 0.10526316, 0.15789474, 0.21052632,\n", + " 0.26315789, 0.31578947, 0.36842105, 0.42105263, 0.47368421,\n", + " 0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211,\n", + " 0.78947368, 0.84210526, 0.89473684, 0.94736842, 1. ])" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.linspace(0,1,20)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Numpy Indexing and Selection\n", + "\n", + "Now you will be given a few matrices, and be asked to replicate the resulting matrix outputs:" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 1, 2, 3, 4, 5],\n", + " [ 6, 7, 8, 9, 10],\n", + " [11, 12, 13, 14, 15],\n", + " [16, 17, 18, 19, 20],\n", + " [21, 22, 23, 24, 25]])" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mat = np.arange(1,26).reshape(5,5)\n", + "mat" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "WRITE CODE HERE THAT REPRODUCES THIS OUTPUT :\n", + "\n", + "array([[12, 13, 14, 15],\n", + " [17, 18, 19, 20],\n", + " [22, 23, 24, 25]])" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[12, 13, 14, 15],\n", + " [17, 18, 19, 20],\n", + " [22, 23, 24, 25]])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mat[2:,1:]" + ] + }, + { + "cell_type": "raw", + "metadata": { + "collapsed": true + }, + "source": [ + "# WRITE CODE HERE THAT REPRODUCES THIS OUTPUT : \n", + "\n", + "20" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mat[3,4]" + ] + }, + { + "cell_type": "raw", + "metadata": { + "collapsed": true + }, + "source": [ + "# WRITE CODE HERE THAT REPRODUCES THE FOLLOWING OUTPUT : \n", + "\n", + "array([[ 2],\n", + " [ 7],\n", + " [12]])" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 2],\n", + " [ 7],\n", + " [12]])" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mat[:3,1:2]" + ] + }, + { + "cell_type": "raw", + "metadata": { + "collapsed": true + }, + "source": [ + "# WRITE CODE HERE THAT REPRODUCES THE FOLLOWING OUTPUT: \n", + "\n", + "array([21, 22, 23, 24, 25])" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([21, 22, 23, 24, 25])" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mat[4]" + ] + }, + { + "cell_type": "raw", + "metadata": { + "collapsed": true + }, + "source": [ + "# WRITE CODE HERE THAT REPRODUCES THE OUTPUT : \n", + "\n", + "array([[16, 17, 18, 19, 20],\n", + " [21, 22, 23, 24, 25]])" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[16, 17, 18, 19, 20],\n", + " [21, 22, 23, 24, 25]])" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mat[3:]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Now do the following" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Get the sum of all the values in mat" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "325" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mat.sum()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Get the standard deviation of the values in mat" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "7.211102550927978" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mat.std()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Get the sum of all the columns in mat" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([55, 60, 65, 70, 75])" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mat.sum(axis=0)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Great Job!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/2_NumPy/README.md b/Py_for_DS_and_ML_Bootcamp/2_NumPy/README.md new file mode 100644 index 0000000..697a1c6 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/2_NumPy/README.md @@ -0,0 +1,14 @@ +# NumPy + +NumPy (or Numpy) is a Linear Algebra Library for Python, the reason it is so important for Data Science with Python is that almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks. + +Numpy is also incredibly fast, as it has bindings to C libraries. For more info on why you would want to use Arrays instead of lists, check out this great [StackOverflow post](http://stackoverflow.com/questions/993984/why-numpy-instead-of-python-lists). + + +Numpy has many built-in functions and capabilities. + +Some of the most important aspects of Numpy are: +- vectors, +- arrays, +- matrices, and +- number generation. diff --git a/Py_for_DS_and_ML_Bootcamp/3_Pandas/01-Introduction to Pandas.ipynb b/Py_for_DS_and_ML_Bootcamp/3_Pandas/01-Introduction to Pandas.ipynb new file mode 100644 index 0000000..80af48b --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/3_Pandas/01-Introduction to Pandas.ipynb @@ -0,0 +1,59 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Introduction to Pandas\n", + "\n", + "In this section of the course we will learn how to use pandas for data analysis. You can think of pandas as an extremely powerful version of Excel, with a lot more features. In this section of the course, you should go through the notebooks in this order:\n", + "\n", + "* Introduction to Pandas\n", + "* Series\n", + "* DataFrames\n", + "* Missing Data\n", + "* GroupBy\n", + "* Merging,Joining,and Concatenating\n", + "* Operations\n", + "* Data Input and Output" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/3_Pandas/02-Series.ipynb b/Py_for_DS_and_ML_Bootcamp/3_Pandas/02-Series.ipynb new file mode 100644 index 0000000..2c67dd4 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/3_Pandas/02-Series.ipynb @@ -0,0 +1,433 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "# Series" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The first main data type we will learn about for pandas is the Series data type. Let's import Pandas and explore the Series object.\n", + "\n", + "A Series is very similar to a NumPy array (in fact it is built on top of the NumPy array object). What differentiates the NumPy array from a Series, is that a Series can have axis labels, meaning it can be indexed by a label, instead of just a number location. It also doesn't need to hold numeric data, it can hold any arbitrary Python Object.\n", + "\n", + "Let's explore this concept through some examples:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating a Series\n", + "\n", + "You can convert a list,numpy array, or dictionary to a Series:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "labels = ['a','b','c']\n", + "my_list = [10,20,30]\n", + "arr = np.array([10,20,30])\n", + "d = {'a':10,'b':20,'c':30}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Using Lists**" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 10\n", + "1 20\n", + "2 30\n", + "dtype: int64" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.Series(data=my_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "a 10\n", + "b 20\n", + "c 30\n", + "dtype: int64" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.Series(data=my_list,index=labels)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "a 10\n", + "b 20\n", + "c 30\n", + "dtype: int64" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.Series(my_list,labels)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### NumPy Arrays " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 10\n", + "1 20\n", + "2 30\n", + "dtype: int64" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.Series(arr)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "a 10\n", + "b 20\n", + "c 30\n", + "dtype: int64" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.Series(arr,labels)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Dictionary**" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "a 10\n", + "b 20\n", + "c 30\n", + "dtype: int64" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.Series(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Data in a Series\n", + "\n", + "A pandas Series can hold a variety of object types:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 a\n", + "1 b\n", + "2 c\n", + "dtype: object" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.Series(data=labels)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 \n", + "1 \n", + "2 \n", + "dtype: object" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Even functions (although unlikely that you will use this)\n", + "pd.Series([sum,print,len])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Using an Index\n", + "\n", + "The key to using a Series is understanding its index. Pandas makes use of these index names or numbers by allowing for fast look ups of information (works like a hash table or dictionary).\n", + "\n", + "Let's see some examples of how to grab information from a Series. Let us create two sereis, ser1 and ser2:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "ser1 = pd.Series([1,2,3,4],index = ['USA', 'Germany','USSR', 'Japan']) " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "USA 1\n", + "Germany 2\n", + "USSR 3\n", + "Japan 4\n", + "dtype: int64" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ser1" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "ser2 = pd.Series([1,2,5,4],index = ['USA', 'Germany','Italy', 'Japan']) " + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "USA 1\n", + "Germany 2\n", + "Italy 5\n", + "Japan 4\n", + "dtype: int64" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ser2" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ser1['USA']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Operations are then also done based off of index:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Germany 4.0\n", + "Italy NaN\n", + "Japan 8.0\n", + "USA 2.0\n", + "USSR NaN\n", + "dtype: float64" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ser1 + ser2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's stop here for now and move on to DataFrames, which will expand on the concept of Series!\n", + "# Great Job!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/3_Pandas/03-DataFrames.ipynb b/Py_for_DS_and_ML_Bootcamp/3_Pandas/03-DataFrames.ipynb new file mode 100644 index 0000000..e7f63f7 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/3_Pandas/03-DataFrames.ipynb @@ -0,0 +1,2433 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# DataFrames\n", + "\n", + "DataFrames are the workhorse of pandas and are directly inspired by the R programming language. We can think of a DataFrame as a bunch of Series objects put together to share the same index. Let's use pandas to explore this topic!" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "from numpy.random import randn\n", + "np.random.seed(101)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.DataFrame(randn(5,4),index='A B C D E'.split(),columns='W X Y Z'.split())" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
A2.7068500.6281330.9079690.503826
B0.651118-0.319318-0.8480770.605965
C-2.0181680.7401220.528813-0.589001
D0.188695-0.758872-0.9332370.955057
E0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "A 2.706850 0.628133 0.907969 0.503826\n", + "B 0.651118 -0.319318 -0.848077 0.605965\n", + "C -2.018168 0.740122 0.528813 -0.589001\n", + "D 0.188695 -0.758872 -0.933237 0.955057\n", + "E 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Selection and Indexing\n", + "\n", + "Let's learn the various methods to grab data from a DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "A 2.706850\n", + "B 0.651118\n", + "C -2.018168\n", + "D 0.188695\n", + "E 0.190794\n", + "Name: W, dtype: float64" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['W']" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WZ
A2.7068500.503826
B0.6511180.605965
C-2.018168-0.589001
D0.1886950.955057
E0.1907940.683509
\n", + "
" + ], + "text/plain": [ + " W Z\n", + "A 2.706850 0.503826\n", + "B 0.651118 0.605965\n", + "C -2.018168 -0.589001\n", + "D 0.188695 0.955057\n", + "E 0.190794 0.683509" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Pass a list of column names\n", + "df[['W','Z']]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "A 2.706850\n", + "B 0.651118\n", + "C -2.018168\n", + "D 0.188695\n", + "E 0.190794\n", + "Name: W, dtype: float64" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# SQL Syntax (NOT RECOMMENDED!)\n", + "df.W" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "DataFrame Columns are just Series" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "pandas.core.series.Series" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(df['W'])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Creating a new column:**" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "df['new'] = df['W'] + df['Y']" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZnew
A2.7068500.6281330.9079690.5038263.614819
B0.651118-0.319318-0.8480770.605965-0.196959
C-2.0181680.7401220.528813-0.589001-1.489355
D0.188695-0.758872-0.9332370.955057-0.744542
E0.1907941.9787572.6059670.6835092.796762
\n", + "
" + ], + "text/plain": [ + " W X Y Z new\n", + "A 2.706850 0.628133 0.907969 0.503826 3.614819\n", + "B 0.651118 -0.319318 -0.848077 0.605965 -0.196959\n", + "C -2.018168 0.740122 0.528813 -0.589001 -1.489355\n", + "D 0.188695 -0.758872 -0.933237 0.955057 -0.744542\n", + "E 0.190794 1.978757 2.605967 0.683509 2.796762" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Removing Columns**" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
A2.7068500.6281330.9079690.503826
B0.651118-0.319318-0.8480770.605965
C-2.0181680.7401220.528813-0.589001
D0.188695-0.758872-0.9332370.955057
E0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "A 2.706850 0.628133 0.907969 0.503826\n", + "B 0.651118 -0.319318 -0.848077 0.605965\n", + "C -2.018168 0.740122 0.528813 -0.589001\n", + "D 0.188695 -0.758872 -0.933237 0.955057\n", + "E 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.drop('new',axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZnew
A2.7068500.6281330.9079690.5038263.614819
B0.651118-0.319318-0.8480770.605965-0.196959
C-2.0181680.7401220.528813-0.589001-1.489355
D0.188695-0.758872-0.9332370.955057-0.744542
E0.1907941.9787572.6059670.6835092.796762
\n", + "
" + ], + "text/plain": [ + " W X Y Z new\n", + "A 2.706850 0.628133 0.907969 0.503826 3.614819\n", + "B 0.651118 -0.319318 -0.848077 0.605965 -0.196959\n", + "C -2.018168 0.740122 0.528813 -0.589001 -1.489355\n", + "D 0.188695 -0.758872 -0.933237 0.955057 -0.744542\n", + "E 0.190794 1.978757 2.605967 0.683509 2.796762" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Not inplace unless specified!\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "df.drop('new',axis=1,inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
A2.7068500.6281330.9079690.503826
B0.651118-0.319318-0.8480770.605965
C-2.0181680.7401220.528813-0.589001
D0.188695-0.758872-0.9332370.955057
E0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "A 2.706850 0.628133 0.907969 0.503826\n", + "B 0.651118 -0.319318 -0.848077 0.605965\n", + "C -2.018168 0.740122 0.528813 -0.589001\n", + "D 0.188695 -0.758872 -0.933237 0.955057\n", + "E 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Can also drop rows this way:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
A2.7068500.6281330.9079690.503826
B0.651118-0.319318-0.8480770.605965
C-2.0181680.7401220.528813-0.589001
D0.188695-0.758872-0.9332370.955057
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "A 2.706850 0.628133 0.907969 0.503826\n", + "B 0.651118 -0.319318 -0.848077 0.605965\n", + "C -2.018168 0.740122 0.528813 -0.589001\n", + "D 0.188695 -0.758872 -0.933237 0.955057" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.drop('E',axis=0)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Selecting Rows**" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "W 2.706850\n", + "X 0.628133\n", + "Y 0.907969\n", + "Z 0.503826\n", + "Name: A, dtype: float64" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.loc['A']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Or select based off of position instead of label " + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "W -2.018168\n", + "X 0.740122\n", + "Y 0.528813\n", + "Z -0.589001\n", + "Name: C, dtype: float64" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.iloc[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Selecting subset of rows and columns **" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-0.8480769834036315" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.loc['B','Y']" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WY
A2.7068500.907969
B0.651118-0.848077
\n", + "
" + ], + "text/plain": [ + " W Y\n", + "A 2.706850 0.907969\n", + "B 0.651118 -0.848077" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.loc[['A','B'],['W','Y']]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Conditional Selection\n", + "\n", + "An important feature of pandas is conditional selection using bracket notation, very similar to numpy:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
A2.7068500.6281330.9079690.503826
B0.651118-0.319318-0.8480770.605965
C-2.0181680.7401220.528813-0.589001
D0.188695-0.758872-0.9332370.955057
E0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "A 2.706850 0.628133 0.907969 0.503826\n", + "B 0.651118 -0.319318 -0.848077 0.605965\n", + "C -2.018168 0.740122 0.528813 -0.589001\n", + "D 0.188695 -0.758872 -0.933237 0.955057\n", + "E 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
ATrueTrueTrueTrue
BTrueFalseFalseTrue
CFalseTrueTrueFalse
DTrueFalseFalseTrue
ETrueTrueTrueTrue
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "A True True True True\n", + "B True False False True\n", + "C False True True False\n", + "D True False False True\n", + "E True True True True" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df>0" + ] + }, + { + "cell_type": "code", + "execution_count": 204, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
A2.7068500.6281330.9079690.503826
B0.651118NaNNaN0.605965
CNaN0.7401220.528813NaN
D0.188695NaNNaN0.955057
E0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "A 2.706850 0.628133 0.907969 0.503826\n", + "B 0.651118 NaN NaN 0.605965\n", + "C NaN 0.740122 0.528813 NaN\n", + "D 0.188695 NaN NaN 0.955057\n", + "E 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 204, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[df>0]" + ] + }, + { + "cell_type": "code", + "execution_count": 205, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
A2.7068500.6281330.9079690.503826
B0.651118-0.319318-0.8480770.605965
D0.188695-0.758872-0.9332370.955057
E0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "A 2.706850 0.628133 0.907969 0.503826\n", + "B 0.651118 -0.319318 -0.848077 0.605965\n", + "D 0.188695 -0.758872 -0.933237 0.955057\n", + "E 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 205, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[df['W']>0]" + ] + }, + { + "cell_type": "code", + "execution_count": 206, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "A 0.907969\n", + "B -0.848077\n", + "D -0.933237\n", + "E 2.605967\n", + "Name: Y, dtype: float64" + ] + }, + "execution_count": 206, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[df['W']>0]['Y']" + ] + }, + { + "cell_type": "code", + "execution_count": 207, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
YX
A0.9079690.628133
B-0.848077-0.319318
D-0.933237-0.758872
E2.6059671.978757
\n", + "
" + ], + "text/plain": [ + " Y X\n", + "A 0.907969 0.628133\n", + "B -0.848077 -0.319318\n", + "D -0.933237 -0.758872\n", + "E 2.605967 1.978757" + ] + }, + "execution_count": 207, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[df['W']>0][['Y','X']]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For two conditions you can use | and & with parenthesis:" + ] + }, + { + "cell_type": "code", + "execution_count": 208, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
E0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "E 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 208, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[(df['W']>0) & (df['Y'] > 1)]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## More Index Details\n", + "\n", + "Let's discuss some more features of indexing, including resetting the index or setting it something else. We'll also talk about index hierarchy!" + ] + }, + { + "cell_type": "code", + "execution_count": 209, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
A2.7068500.6281330.9079690.503826
B0.651118-0.319318-0.8480770.605965
C-2.0181680.7401220.528813-0.589001
D0.188695-0.758872-0.9332370.955057
E0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "A 2.706850 0.628133 0.907969 0.503826\n", + "B 0.651118 -0.319318 -0.848077 0.605965\n", + "C -2.018168 0.740122 0.528813 -0.589001\n", + "D 0.188695 -0.758872 -0.933237 0.955057\n", + "E 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 209, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 210, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
indexWXYZ
0A2.7068500.6281330.9079690.503826
1B0.651118-0.319318-0.8480770.605965
2C-2.0181680.7401220.528813-0.589001
3D0.188695-0.758872-0.9332370.955057
4E0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " index W X Y Z\n", + "0 A 2.706850 0.628133 0.907969 0.503826\n", + "1 B 0.651118 -0.319318 -0.848077 0.605965\n", + "2 C -2.018168 0.740122 0.528813 -0.589001\n", + "3 D 0.188695 -0.758872 -0.933237 0.955057\n", + "4 E 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 210, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Reset to default 0,1...n index\n", + "df.reset_index()" + ] + }, + { + "cell_type": "code", + "execution_count": 211, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "newind = 'CA NY WY OR CO'.split()" + ] + }, + { + "cell_type": "code", + "execution_count": 212, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "df['States'] = newind" + ] + }, + { + "cell_type": "code", + "execution_count": 213, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZStates
A2.7068500.6281330.9079690.503826CA
B0.651118-0.319318-0.8480770.605965NY
C-2.0181680.7401220.528813-0.589001WY
D0.188695-0.758872-0.9332370.955057OR
E0.1907941.9787572.6059670.683509CO
\n", + "
" + ], + "text/plain": [ + " W X Y Z States\n", + "A 2.706850 0.628133 0.907969 0.503826 CA\n", + "B 0.651118 -0.319318 -0.848077 0.605965 NY\n", + "C -2.018168 0.740122 0.528813 -0.589001 WY\n", + "D 0.188695 -0.758872 -0.933237 0.955057 OR\n", + "E 0.190794 1.978757 2.605967 0.683509 CO" + ] + }, + "execution_count": 213, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 214, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
States
CA2.7068500.6281330.9079690.503826
NY0.651118-0.319318-0.8480770.605965
WY-2.0181680.7401220.528813-0.589001
OR0.188695-0.758872-0.9332370.955057
CO0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "States \n", + "CA 2.706850 0.628133 0.907969 0.503826\n", + "NY 0.651118 -0.319318 -0.848077 0.605965\n", + "WY -2.018168 0.740122 0.528813 -0.589001\n", + "OR 0.188695 -0.758872 -0.933237 0.955057\n", + "CO 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 214, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.set_index('States')" + ] + }, + { + "cell_type": "code", + "execution_count": 215, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZStates
A2.7068500.6281330.9079690.503826CA
B0.651118-0.319318-0.8480770.605965NY
C-2.0181680.7401220.528813-0.589001WY
D0.188695-0.758872-0.9332370.955057OR
E0.1907941.9787572.6059670.683509CO
\n", + "
" + ], + "text/plain": [ + " W X Y Z States\n", + "A 2.706850 0.628133 0.907969 0.503826 CA\n", + "B 0.651118 -0.319318 -0.848077 0.605965 NY\n", + "C -2.018168 0.740122 0.528813 -0.589001 WY\n", + "D 0.188695 -0.758872 -0.933237 0.955057 OR\n", + "E 0.190794 1.978757 2.605967 0.683509 CO" + ] + }, + "execution_count": 215, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 216, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "df.set_index('States',inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 218, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WXYZ
States
CA2.7068500.6281330.9079690.503826
NY0.651118-0.319318-0.8480770.605965
WY-2.0181680.7401220.528813-0.589001
OR0.188695-0.758872-0.9332370.955057
CO0.1907941.9787572.6059670.683509
\n", + "
" + ], + "text/plain": [ + " W X Y Z\n", + "States \n", + "CA 2.706850 0.628133 0.907969 0.503826\n", + "NY 0.651118 -0.319318 -0.848077 0.605965\n", + "WY -2.018168 0.740122 0.528813 -0.589001\n", + "OR 0.188695 -0.758872 -0.933237 0.955057\n", + "CO 0.190794 1.978757 2.605967 0.683509" + ] + }, + "execution_count": 218, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Multi-Index and Index Hierarchy\n", + "\n", + "Let us go over how to work with Multi-Index, first we'll create a quick example of what a Multi-Indexed DataFrame would look like:" + ] + }, + { + "cell_type": "code", + "execution_count": 253, + "metadata": {}, + "outputs": [], + "source": [ + "# Index Levels\n", + "outside = ['G1','G1','G1','G2','G2','G2']\n", + "inside = [1,2,3,1,2,3]\n", + "hier_index = list(zip(outside,inside))\n", + "hier_index = pd.MultiIndex.from_tuples(hier_index)" + ] + }, + { + "cell_type": "code", + "execution_count": 254, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "MultiIndex(levels=[['G1', 'G2'], [1, 2, 3]],\n", + " labels=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]])" + ] + }, + "execution_count": 254, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hier_index" + ] + }, + { + "cell_type": "code", + "execution_count": 257, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
AB
G110.1536610.167638
2-0.7659300.962299
30.902826-0.537909
G21-1.5496710.435253
21.259904-0.447898
30.2662070.412580
\n", + "
" + ], + "text/plain": [ + " A B\n", + "G1 1 0.153661 0.167638\n", + " 2 -0.765930 0.962299\n", + " 3 0.902826 -0.537909\n", + "G2 1 -1.549671 0.435253\n", + " 2 1.259904 -0.447898\n", + " 3 0.266207 0.412580" + ] + }, + "execution_count": 257, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df = pd.DataFrame(np.random.randn(6,2),index=hier_index,columns=['A','B'])\n", + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's show how to index this! For index hierarchy we use df.loc[], if this was on the columns axis, you would just use normal bracket notation df[]. Calling one level of the index returns the sub-dataframe:" + ] + }, + { + "cell_type": "code", + "execution_count": 260, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
AB
10.1536610.167638
2-0.7659300.962299
30.902826-0.537909
\n", + "
" + ], + "text/plain": [ + " A B\n", + "1 0.153661 0.167638\n", + "2 -0.765930 0.962299\n", + "3 0.902826 -0.537909" + ] + }, + "execution_count": 260, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.loc['G1']" + ] + }, + { + "cell_type": "code", + "execution_count": 263, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "A 0.153661\n", + "B 0.167638\n", + "Name: 1, dtype: float64" + ] + }, + "execution_count": 263, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.loc['G1'].loc[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 265, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "FrozenList([None, None])" + ] + }, + "execution_count": 265, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.index.names" + ] + }, + { + "cell_type": "code", + "execution_count": 266, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "df.index.names = ['Group','Num']" + ] + }, + { + "cell_type": "code", + "execution_count": 267, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
AB
GroupNum
G110.1536610.167638
2-0.7659300.962299
30.902826-0.537909
G21-1.5496710.435253
21.259904-0.447898
30.2662070.412580
\n", + "
" + ], + "text/plain": [ + " A B\n", + "Group Num \n", + "G1 1 0.153661 0.167638\n", + " 2 -0.765930 0.962299\n", + " 3 0.902826 -0.537909\n", + "G2 1 -1.549671 0.435253\n", + " 2 1.259904 -0.447898\n", + " 3 0.266207 0.412580" + ] + }, + "execution_count": 267, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 270, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
AB
Num
10.1536610.167638
2-0.7659300.962299
30.902826-0.537909
\n", + "
" + ], + "text/plain": [ + " A B\n", + "Num \n", + "1 0.153661 0.167638\n", + "2 -0.765930 0.962299\n", + "3 0.902826 -0.537909" + ] + }, + "execution_count": 270, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.xs('G1')" + ] + }, + { + "cell_type": "code", + "execution_count": 271, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "A 0.153661\n", + "B 0.167638\n", + "Name: (G1, 1), dtype: float64" + ] + }, + "execution_count": 271, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.xs(['G1',1])" + ] + }, + { + "cell_type": "code", + "execution_count": 273, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
AB
Group
G10.1536610.167638
G2-1.5496710.435253
\n", + "
" + ], + "text/plain": [ + " A B\n", + "Group \n", + "G1 0.153661 0.167638\n", + "G2 -1.549671 0.435253" + ] + }, + "execution_count": 273, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.xs(1,level='Num')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/3_Pandas/04-Missing Data.ipynb b/Py_for_DS_and_ML_Bootcamp/3_Pandas/04-Missing Data.ipynb new file mode 100644 index 0000000..b173c1c --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/3_Pandas/04-Missing Data.ipynb @@ -0,0 +1,453 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Missing Data\n", + "\n", + "Let's show a few convenient methods to deal with Missing Data in pandas:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.DataFrame({'A':[1,2,np.nan],\n", + " 'B':[5,np.nan,np.nan],\n", + " 'C':[1,2,3]})" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABC
01.05.01
12.0NaN2
2NaNNaN3
\n", + "
" + ], + "text/plain": [ + " A B C\n", + "0 1.0 5.0 1\n", + "1 2.0 NaN 2\n", + "2 NaN NaN 3" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABC
01.05.01
\n", + "
" + ], + "text/plain": [ + " A B C\n", + "0 1.0 5.0 1" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.dropna()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
C
01
12
23
\n", + "
" + ], + "text/plain": [ + " C\n", + "0 1\n", + "1 2\n", + "2 3" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.dropna(axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABC
01.05.01
12.0NaN2
\n", + "
" + ], + "text/plain": [ + " A B C\n", + "0 1.0 5.0 1\n", + "1 2.0 NaN 2" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.dropna(thresh=2)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABC
0151
12FILL VALUE2
2FILL VALUEFILL VALUE3
\n", + "
" + ], + "text/plain": [ + " A B C\n", + "0 1 5 1\n", + "1 2 FILL VALUE 2\n", + "2 FILL VALUE FILL VALUE 3" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.fillna(value='FILL VALUE')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABC
01.05.01
12.01.52
21.51.53
\n", + "
" + ], + "text/plain": [ + " A B C\n", + "0 1.0 5.0 1\n", + "1 2.0 1.5 2\n", + "2 1.5 1.5 3" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.fillna(value=df['A'].mean())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/3_Pandas/05-Groupby.ipynb b/Py_for_DS_and_ML_Bootcamp/3_Pandas/05-Groupby.ipynb new file mode 100644 index 0000000..a7ee5e8 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/3_Pandas/05-Groupby.ipynb @@ -0,0 +1,208 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Groupby\n", + "\n", + "The groupby method allows you to group rows of data together and call aggregate functions" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "# Create dataframe\n", + "data = {'Company':['GOOG','GOOG','MSFT','MSFT','FB','FB'],\n", + " 'Person':['Sam','Charlie','Amy','Vanessa','Carl','Sarah'],\n", + " 'Sales':[200,120,340,124,243,350]}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.DataFrame(data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Now you can use the .groupby() method to group rows together based off of a column name. For instance let's group based off of Company. This will create a DataFrameGroupBy object:**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df.groupby('Company')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can save this object as a new variable:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "by_comp = df.groupby(\"Company\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And then call aggregate methods off the object:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "by_comp.mean()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df.groupby('Company').mean()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "More examples of aggregate methods:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "by_comp.std()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "by_comp.min()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "by_comp.max()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "by_comp.count()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "by_comp.describe()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "by_comp.describe().transpose()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "by_comp.describe().transpose()['GOOG']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/3_Pandas/06-Merging, Joining, and Concatenating.ipynb b/Py_for_DS_and_ML_Bootcamp/3_Pandas/06-Merging, Joining, and Concatenating.ipynb new file mode 100644 index 0000000..934cb11 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/3_Pandas/06-Merging, Joining, and Concatenating.ipynb @@ -0,0 +1,1629 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Merging, Joining, and Concatenating\n", + "\n", + "There are 3 main ways of combining DataFrames together: Merging, Joining and Concatenating. In this lecture we will discuss these 3 methods with examples.\n", + "\n", + "____" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Example DataFrames" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],\n", + " 'B': ['B0', 'B1', 'B2', 'B3'],\n", + " 'C': ['C0', 'C1', 'C2', 'C3'],\n", + " 'D': ['D0', 'D1', 'D2', 'D3']},\n", + " index=[0, 1, 2, 3])" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],\n", + " 'B': ['B4', 'B5', 'B6', 'B7'],\n", + " 'C': ['C4', 'C5', 'C6', 'C7'],\n", + " 'D': ['D4', 'D5', 'D6', 'D7']},\n", + " index=[4, 5, 6, 7]) " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "df3 = pd.DataFrame({'A': ['A8', 'A9', 'A10', 'A11'],\n", + " 'B': ['B8', 'B9', 'B10', 'B11'],\n", + " 'C': ['C8', 'C9', 'C10', 'C11'],\n", + " 'D': ['D8', 'D9', 'D10', 'D11']},\n", + " index=[8, 9, 10, 11])" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCD
0A0B0C0D0
1A1B1C1D1
2A2B2C2D2
3A3B3C3D3
\n", + "
" + ], + "text/plain": [ + " A B C D\n", + "0 A0 B0 C0 D0\n", + "1 A1 B1 C1 D1\n", + "2 A2 B2 C2 D2\n", + "3 A3 B3 C3 D3" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df1" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCD
4A4B4C4D4
5A5B5C5D5
6A6B6C6D6
7A7B7C7D7
\n", + "
" + ], + "text/plain": [ + " A B C D\n", + "4 A4 B4 C4 D4\n", + "5 A5 B5 C5 D5\n", + "6 A6 B6 C6 D6\n", + "7 A7 B7 C7 D7" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCD
8A8B8C8D8
9A9B9C9D9
10A10B10C10D10
11A11B11C11D11
\n", + "
" + ], + "text/plain": [ + " A B C D\n", + "8 A8 B8 C8 D8\n", + "9 A9 B9 C9 D9\n", + "10 A10 B10 C10 D10\n", + "11 A11 B11 C11 D11" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Concatenation\n", + "\n", + "Concatenation basically glues together DataFrames. Keep in mind that dimensions should match along the axis you are concatenating on. You can use **pd.concat** and pass in a list of DataFrames to concatenate together:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCD
0A0B0C0D0
1A1B1C1D1
2A2B2C2D2
3A3B3C3D3
4A4B4C4D4
5A5B5C5D5
6A6B6C6D6
7A7B7C7D7
8A8B8C8D8
9A9B9C9D9
10A10B10C10D10
11A11B11C11D11
\n", + "
" + ], + "text/plain": [ + " A B C D\n", + "0 A0 B0 C0 D0\n", + "1 A1 B1 C1 D1\n", + "2 A2 B2 C2 D2\n", + "3 A3 B3 C3 D3\n", + "4 A4 B4 C4 D4\n", + "5 A5 B5 C5 D5\n", + "6 A6 B6 C6 D6\n", + "7 A7 B7 C7 D7\n", + "8 A8 B8 C8 D8\n", + "9 A9 B9 C9 D9\n", + "10 A10 B10 C10 D10\n", + "11 A11 B11 C11 D11" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.concat([df1,df2,df3])" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCDABCDABCD
0A0B0C0D0NaNNaNNaNNaNNaNNaNNaNNaN
1A1B1C1D1NaNNaNNaNNaNNaNNaNNaNNaN
2A2B2C2D2NaNNaNNaNNaNNaNNaNNaNNaN
3A3B3C3D3NaNNaNNaNNaNNaNNaNNaNNaN
4NaNNaNNaNNaNA4B4C4D4NaNNaNNaNNaN
5NaNNaNNaNNaNA5B5C5D5NaNNaNNaNNaN
6NaNNaNNaNNaNA6B6C6D6NaNNaNNaNNaN
7NaNNaNNaNNaNA7B7C7D7NaNNaNNaNNaN
8NaNNaNNaNNaNNaNNaNNaNNaNA8B8C8D8
9NaNNaNNaNNaNNaNNaNNaNNaNA9B9C9D9
10NaNNaNNaNNaNNaNNaNNaNNaNA10B10C10D10
11NaNNaNNaNNaNNaNNaNNaNNaNA11B11C11D11
\n", + "
" + ], + "text/plain": [ + " A B C D A B C D A B C D\n", + "0 A0 B0 C0 D0 NaN NaN NaN NaN NaN NaN NaN NaN\n", + "1 A1 B1 C1 D1 NaN NaN NaN NaN NaN NaN NaN NaN\n", + "2 A2 B2 C2 D2 NaN NaN NaN NaN NaN NaN NaN NaN\n", + "3 A3 B3 C3 D3 NaN NaN NaN NaN NaN NaN NaN NaN\n", + "4 NaN NaN NaN NaN A4 B4 C4 D4 NaN NaN NaN NaN\n", + "5 NaN NaN NaN NaN A5 B5 C5 D5 NaN NaN NaN NaN\n", + "6 NaN NaN NaN NaN A6 B6 C6 D6 NaN NaN NaN NaN\n", + "7 NaN NaN NaN NaN A7 B7 C7 D7 NaN NaN NaN NaN\n", + "8 NaN NaN NaN NaN NaN NaN NaN NaN A8 B8 C8 D8\n", + "9 NaN NaN NaN NaN NaN NaN NaN NaN A9 B9 C9 D9\n", + "10 NaN NaN NaN NaN NaN NaN NaN NaN A10 B10 C10 D10\n", + "11 NaN NaN NaN NaN NaN NaN NaN NaN A11 B11 C11 D11" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.concat([df1,df2,df3],axis=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "_____\n", + "## Example DataFrames" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],\n", + " 'A': ['A0', 'A1', 'A2', 'A3'],\n", + " 'B': ['B0', 'B1', 'B2', 'B3']})\n", + " \n", + "right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],\n", + " 'C': ['C0', 'C1', 'C2', 'C3'],\n", + " 'D': ['D0', 'D1', 'D2', 'D3']}) " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
keyAB
0K0A0B0
1K1A1B1
2K2A2B2
3K3A3B3
\n", + "
" + ], + "text/plain": [ + " key A B\n", + "0 K0 A0 B0\n", + "1 K1 A1 B1\n", + "2 K2 A2 B2\n", + "3 K3 A3 B3" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "left" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
keyCD
0K0C0D0
1K1C1D1
2K2C2D2
3K3C3D3
\n", + "
" + ], + "text/plain": [ + " key C D\n", + "0 K0 C0 D0\n", + "1 K1 C1 D1\n", + "2 K2 C2 D2\n", + "3 K3 C3 D3" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "right" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Merging\n", + "\n", + "The **merge** function allows you to merge DataFrames together using a similar logic as merging SQL Tables together. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
keyABCD
0K0A0B0C0D0
1K1A1B1C1D1
2K2A2B2C2D2
3K3A3B3C3D3
\n", + "
" + ], + "text/plain": [ + " key A B C D\n", + "0 K0 A0 B0 C0 D0\n", + "1 K1 A1 B1 C1 D1\n", + "2 K2 A2 B2 C2 D2\n", + "3 K3 A3 B3 C3 D3" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.merge(left,right,how='inner',on='key')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Or to show a more complicated example:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],\n", + " 'key2': ['K0', 'K1', 'K0', 'K1'],\n", + " 'A': ['A0', 'A1', 'A2', 'A3'],\n", + " 'B': ['B0', 'B1', 'B2', 'B3']})\n", + " \n", + "right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],\n", + " 'key2': ['K0', 'K0', 'K0', 'K0'],\n", + " 'C': ['C0', 'C1', 'C2', 'C3'],\n", + " 'D': ['D0', 'D1', 'D2', 'D3']})" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
key1key2ABCD
0K0K0A0B0C0D0
1K1K0A2B2C1D1
2K1K0A2B2C2D2
\n", + "
" + ], + "text/plain": [ + " key1 key2 A B C D\n", + "0 K0 K0 A0 B0 C0 D0\n", + "1 K1 K0 A2 B2 C1 D1\n", + "2 K1 K0 A2 B2 C2 D2" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.merge(left, right, on=['key1', 'key2'])" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
key1key2ABCD
0K0K0A0B0C0D0
1K0K1A1B1NaNNaN
2K1K0A2B2C1D1
3K1K0A2B2C2D2
4K2K1A3B3NaNNaN
5K2K0NaNNaNC3D3
\n", + "
" + ], + "text/plain": [ + " key1 key2 A B C D\n", + "0 K0 K0 A0 B0 C0 D0\n", + "1 K0 K1 A1 B1 NaN NaN\n", + "2 K1 K0 A2 B2 C1 D1\n", + "3 K1 K0 A2 B2 C2 D2\n", + "4 K2 K1 A3 B3 NaN NaN\n", + "5 K2 K0 NaN NaN C3 D3" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.merge(left, right, how='outer', on=['key1', 'key2'])" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
key1key2ABCD
0K0K0A0B0C0D0
1K1K0A2B2C1D1
2K1K0A2B2C2D2
3K2K0NaNNaNC3D3
\n", + "
" + ], + "text/plain": [ + " key1 key2 A B C D\n", + "0 K0 K0 A0 B0 C0 D0\n", + "1 K1 K0 A2 B2 C1 D1\n", + "2 K1 K0 A2 B2 C2 D2\n", + "3 K2 K0 NaN NaN C3 D3" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.merge(left, right, how='right', on=['key1', 'key2'])" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
key1key2ABCD
0K0K0A0B0C0D0
1K0K1A1B1NaNNaN
2K1K0A2B2C1D1
3K1K0A2B2C2D2
4K2K1A3B3NaNNaN
\n", + "
" + ], + "text/plain": [ + " key1 key2 A B C D\n", + "0 K0 K0 A0 B0 C0 D0\n", + "1 K0 K1 A1 B1 NaN NaN\n", + "2 K1 K0 A2 B2 C1 D1\n", + "3 K1 K0 A2 B2 C2 D2\n", + "4 K2 K1 A3 B3 NaN NaN" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.merge(left, right, how='left', on=['key1', 'key2'])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Joining\n", + "Joining is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "left = pd.DataFrame({'A': ['A0', 'A1', 'A2'],\n", + " 'B': ['B0', 'B1', 'B2']},\n", + " index=['K0', 'K1', 'K2']) \n", + "\n", + "right = pd.DataFrame({'C': ['C0', 'C2', 'C3'],\n", + " 'D': ['D0', 'D2', 'D3']},\n", + " index=['K0', 'K2', 'K3'])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCD
K0A0B0C0D0
K1A1B1NaNNaN
K2A2B2C2D2
\n", + "
" + ], + "text/plain": [ + " A B C D\n", + "K0 A0 B0 C0 D0\n", + "K1 A1 B1 NaN NaN\n", + "K2 A2 B2 C2 D2" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "left.join(right)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCD
K0A0B0C0D0
K1A1B1NaNNaN
K2A2B2C2D2
K3NaNNaNC3D3
\n", + "
" + ], + "text/plain": [ + " A B C D\n", + "K0 A0 B0 C0 D0\n", + "K1 A1 B1 NaN NaN\n", + "K2 A2 B2 C2 D2\n", + "K3 NaN NaN C3 D3" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "left.join(right, how='outer')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/3_Pandas/07-Operations.ipynb b/Py_for_DS_and_ML_Bootcamp/3_Pandas/07-Operations.ipynb new file mode 100644 index 0000000..c757d06 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/3_Pandas/07-Operations.ipynb @@ -0,0 +1,1025 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Operations\n", + "\n", + "There are lots of operations with pandas that will be really useful to you, but don't fall into any distinct category. Let's show them here in this lecture:" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col1col2col3
01444abc
12555def
23666ghi
34444xyz
\n", + "
" + ], + "text/plain": [ + " col1 col2 col3\n", + "0 1 444 abc\n", + "1 2 555 def\n", + "2 3 666 ghi\n", + "3 4 444 xyz" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "df = pd.DataFrame({'col1':[1,2,3,4],'col2':[444,555,666,444],'col3':['abc','def','ghi','xyz']})\n", + "df.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Info on Unique Values" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([444, 555, 666])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['col2'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['col2'].nunique()" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "444 2\n", + "555 1\n", + "666 1\n", + "Name: col2, dtype: int64" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['col2'].value_counts()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Selecting Data" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "#Select from DataFrame using criteria from multiple columns\n", + "newdf = df[(df['col1']>2) & (df['col2']==444)]" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col1col2col3
34444xyz
\n", + "
" + ], + "text/plain": [ + " col1 col2 col3\n", + "3 4 444 xyz" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "newdf" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Applying Functions" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def times2(x):\n", + " return x*2" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 2\n", + "1 4\n", + "2 6\n", + "3 8\n", + "Name: col1, dtype: int64" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['col1'].apply(times2)" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 3\n", + "1 3\n", + "2 3\n", + "3 3\n", + "Name: col3, dtype: int64" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['col3'].apply(len)" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['col1'].sum()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Permanently Removing a Column**" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "del df['col1']" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col2col3
0444abc
1555def
2666ghi
3444xyz
\n", + "
" + ], + "text/plain": [ + " col2 col3\n", + "0 444 abc\n", + "1 555 def\n", + "2 666 ghi\n", + "3 444 xyz" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Get column and index names: **" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['col2', 'col3'], dtype='object')" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.columns" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "RangeIndex(start=0, stop=4, step=1)" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.index" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Sorting and Ordering a DataFrame:**" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col2col3
0444abc
1555def
2666ghi
3444xyz
\n", + "
" + ], + "text/plain": [ + " col2 col3\n", + "0 444 abc\n", + "1 555 def\n", + "2 666 ghi\n", + "3 444 xyz" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col2col3
0444abc
3444xyz
1555def
2666ghi
\n", + "
" + ], + "text/plain": [ + " col2 col3\n", + "0 444 abc\n", + "3 444 xyz\n", + "1 555 def\n", + "2 666 ghi" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.sort_values(by='col2') #inplace=False by default" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Find Null Values or Check for Null Values**" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col2col3
0FalseFalse
1FalseFalse
2FalseFalse
3FalseFalse
\n", + "
" + ], + "text/plain": [ + " col2 col3\n", + "0 False False\n", + "1 False False\n", + "2 False False\n", + "3 False False" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.isnull()" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col2col3
0444abc
1555def
2666ghi
3444xyz
\n", + "
" + ], + "text/plain": [ + " col2 col3\n", + "0 444 abc\n", + "1 555 def\n", + "2 666 ghi\n", + "3 444 xyz" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Drop rows with NaN Values\n", + "df.dropna()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Filling in NaN values with something else: **" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col1col2col3
01.0NaNabc
12.0555.0def
23.0666.0ghi
3NaN444.0xyz
\n", + "
" + ], + "text/plain": [ + " col1 col2 col3\n", + "0 1.0 NaN abc\n", + "1 2.0 555.0 def\n", + "2 3.0 666.0 ghi\n", + "3 NaN 444.0 xyz" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df = pd.DataFrame({'col1':[1,2,3,np.nan],\n", + " 'col2':[np.nan,555,666,444],\n", + " 'col3':['abc','def','ghi','xyz']})\n", + "df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col1col2col3
01FILLabc
12555def
23666ghi
3FILL444xyz
\n", + "
" + ], + "text/plain": [ + " col1 col2 col3\n", + "0 1 FILL abc\n", + "1 2 555 def\n", + "2 3 666 ghi\n", + "3 FILL 444 xyz" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.fillna('FILL')" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "data = {'A':['foo','foo','foo','bar','bar','bar'],\n", + " 'B':['one','one','two','two','one','one'],\n", + " 'C':['x','y','x','y','x','y'],\n", + " 'D':[1,3,2,5,4,1]}\n", + "\n", + "df = pd.DataFrame(data)" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ABCD
0fooonex1
1foooney3
2footwox2
3bartwoy5
4baronex4
5baroney1
\n", + "
" + ], + "text/plain": [ + " A B C D\n", + "0 foo one x 1\n", + "1 foo one y 3\n", + "2 foo two x 2\n", + "3 bar two y 5\n", + "4 bar one x 4\n", + "5 bar one y 1" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Cxy
AB
barone4.01.0
twoNaN5.0
fooone1.03.0
two2.0NaN
\n", + "
" + ], + "text/plain": [ + "C x y\n", + "A B \n", + "bar one 4.0 1.0\n", + " two NaN 5.0\n", + "foo one 1.0 3.0\n", + " two 2.0 NaN" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.pivot_table(values='D',index=['A', 'B'],columns=['C'])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/3_Pandas/08-Data Input and Output.ipynb b/Py_for_DS_and_ML_Bootcamp/3_Pandas/08-Data Input and Output.ipynb new file mode 100644 index 0000000..fd05687 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/3_Pandas/08-Data Input and Output.ipynb @@ -0,0 +1,780 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Data Input and Output\n", + "\n", + "This notebook is the reference code for getting input and output, pandas can read a variety of file types using its pd.read_ methods. Let's take a look at the most common data types:" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## CSV\n", + "\n", + "### CSV Input" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
abcd
00123
14567
2891011
312131415
\n", + "
" + ], + "text/plain": [ + " a b c d\n", + "0 0 1 2 3\n", + "1 4 5 6 7\n", + "2 8 9 10 11\n", + "3 12 13 14 15" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df = pd.read_csv('../data/example')\n", + "\n", + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### CSV Output" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "df.to_csv('example',index=False)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Excel\n", + "Pandas can read and write excel files, keep in mind, this only imports data. Not formulas or images, having images or macros may cause this read_excel method to crash. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Excel Input" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Unnamed: 0abcd
000123
114567
22891011
3312131415
\n", + "
" + ], + "text/plain": [ + " Unnamed: 0 a b c d\n", + "0 0 0 1 2 3\n", + "1 1 4 5 6 7\n", + "2 2 8 9 10 11\n", + "3 3 12 13 14 15" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.read_excel('../data/Excel_Sample.xlsx',sheet_name='Sheet1')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Excel Output" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "df.to_excel('Excel_Sample.xlsx',sheet_name='Sheet1')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## HTML\n", + "\n", + "You may need to install htmllib5,lxml, and BeautifulSoup4. In your terminal/command prompt run:\n", + "\n", + " conda install lxml\n", + " conda install html5lib\n", + " conda install BeautifulSoup4\n", + "\n", + "Then restart Jupyter Notebook.\n", + "(or use pip install if you aren't using the Anaconda Distribution)\n", + "\n", + "Pandas can read table tabs off of html. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install lxml\n", + "# !pip install html5lib\n", + "# !pip install BeautifulSoup4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### HTML Input\n", + "\n", + "Pandas read_html function will read tables off of a webpage and return a list of DataFrame objects:" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_html('http://www.fdic.gov/bank/individual/failed/banklist.html')" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Bank NameCitySTCERTAcquiring InstitutionClosing Date
0The First State BankBarboursvilleWV14361MVB Bank, Inc.April 3, 2020
1Ericson State BankEricsonNE18265Farmers and Merchants BankFebruary 14, 2020
2City National Bank of New JerseyNewarkNJ21111Industrial BankNovember 1, 2019
3Resolute BankMaumeeOH58317Buckeye State BankOctober 25, 2019
4Louisa Community BankLouisaKY58112Kentucky Farmers Bank CorporationOctober 25, 2019
.....................
556Superior Bank, FSBHinsdaleIL32646Superior Federal, FSBJuly 27, 2001
557Malta National BankMaltaOH6629North Valley BankMay 3, 2001
558First Alliance Bank & Trust Co.ManchesterNH34264Southern New Hampshire Bank & TrustFebruary 2, 2001
559National State Bank of MetropolisMetropolisIL3815Banterra Bank of MarionDecember 14, 2000
560Bank of HonoluluHonoluluHI21029Bank of the OrientOctober 13, 2000
\n", + "

561 rows × 6 columns

\n", + "
" + ], + "text/plain": [ + " Bank Name City ST CERT \\\n", + "0 The First State Bank Barboursville WV 14361 \n", + "1 Ericson State Bank Ericson NE 18265 \n", + "2 City National Bank of New Jersey Newark NJ 21111 \n", + "3 Resolute Bank Maumee OH 58317 \n", + "4 Louisa Community Bank Louisa KY 58112 \n", + ".. ... ... .. ... \n", + "556 Superior Bank, FSB Hinsdale IL 32646 \n", + "557 Malta National Bank Malta OH 6629 \n", + "558 First Alliance Bank & Trust Co. Manchester NH 34264 \n", + "559 National State Bank of Metropolis Metropolis IL 3815 \n", + "560 Bank of Honolulu Honolulu HI 21029 \n", + "\n", + " Acquiring Institution Closing Date \n", + "0 MVB Bank, Inc. April 3, 2020 \n", + "1 Farmers and Merchants Bank February 14, 2020 \n", + "2 Industrial Bank November 1, 2019 \n", + "3 Buckeye State Bank October 25, 2019 \n", + "4 Kentucky Farmers Bank Corporation October 25, 2019 \n", + ".. ... ... \n", + "556 Superior Federal, FSB July 27, 2001 \n", + "557 North Valley Bank May 3, 2001 \n", + "558 Southern New Hampshire Bank & Trust February 2, 2001 \n", + "559 Banterra Bank of Marion December 14, 2000 \n", + "560 Bank of the Orient October 13, 2000 \n", + "\n", + "[561 rows x 6 columns]" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "failed_banks = pd.DataFrame(df[0])\n", + "\n", + "failed_banks" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "____" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "_____\n", + "# SQL (Optional)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The pandas.io.sql module provides a collection of query wrappers to both facilitate data retrieval and to reduce dependency on DB-specific API. Database abstraction is provided by SQLAlchemy if installed. In addition you will need a driver library for your database. Examples of such drivers are psycopg2 for PostgreSQL or pymysql for MySQL. For SQLite this is included in Python’s standard library by default. You can find an overview of supported drivers for each SQL dialect in the SQLAlchemy docs.\n", + "\n", + "\n", + "If SQLAlchemy is not installed, a fallback is only provided for sqlite (and for mysql for backwards compatibility, but this is deprecated and will be removed in a future version). This mode requires a Python database adapter which respect the Python DB-API.\n", + "\n", + "See also some cookbook examples for some advanced strategies.\n", + "\n", + "The key functions are:\n", + "\n", + "* read_sql_table(table_name, con[, schema, ...])\t\n", + " * Read SQL database table into a DataFrame.\n", + "* read_sql_query(sql, con[, index_col, ...])\t\n", + " * Read SQL query into a DataFrame.\n", + "* read_sql(sql, con[, index_col, ...])\t\n", + " * Read SQL query or database table into a DataFrame.\n", + "* DataFrame.to_sql(name, con[, flavor, ...])\t\n", + " * Write records stored in a DataFrame to a SQL database." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "from sqlalchemy import create_engine" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "engine = create_engine('sqlite://', echo=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [], + "source": [ + "failed_banks.to_sql('failed_banks', con=engine)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "sql_df = pd.read_sql('failed_banks',con=engine)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
indexBank NameCitySTCERTAcquiring InstitutionClosing Date
00The First State BankBarboursvilleWV14361MVB Bank, Inc.April 3, 2020
11Ericson State BankEricsonNE18265Farmers and Merchants BankFebruary 14, 2020
22City National Bank of New JerseyNewarkNJ21111Industrial BankNovember 1, 2019
33Resolute BankMaumeeOH58317Buckeye State BankOctober 25, 2019
44Louisa Community BankLouisaKY58112Kentucky Farmers Bank CorporationOctober 25, 2019
........................
556556Superior Bank, FSBHinsdaleIL32646Superior Federal, FSBJuly 27, 2001
557557Malta National BankMaltaOH6629North Valley BankMay 3, 2001
558558First Alliance Bank & Trust Co.ManchesterNH34264Southern New Hampshire Bank & TrustFebruary 2, 2001
559559National State Bank of MetropolisMetropolisIL3815Banterra Bank of MarionDecember 14, 2000
560560Bank of HonoluluHonoluluHI21029Bank of the OrientOctober 13, 2000
\n", + "

561 rows × 7 columns

\n", + "
" + ], + "text/plain": [ + " index Bank Name City ST CERT \\\n", + "0 0 The First State Bank Barboursville WV 14361 \n", + "1 1 Ericson State Bank Ericson NE 18265 \n", + "2 2 City National Bank of New Jersey Newark NJ 21111 \n", + "3 3 Resolute Bank Maumee OH 58317 \n", + "4 4 Louisa Community Bank Louisa KY 58112 \n", + ".. ... ... ... .. ... \n", + "556 556 Superior Bank, FSB Hinsdale IL 32646 \n", + "557 557 Malta National Bank Malta OH 6629 \n", + "558 558 First Alliance Bank & Trust Co. Manchester NH 34264 \n", + "559 559 National State Bank of Metropolis Metropolis IL 3815 \n", + "560 560 Bank of Honolulu Honolulu HI 21029 \n", + "\n", + " Acquiring Institution Closing Date \n", + "0 MVB Bank, Inc. April 3, 2020 \n", + "1 Farmers and Merchants Bank February 14, 2020 \n", + "2 Industrial Bank November 1, 2019 \n", + "3 Buckeye State Bank October 25, 2019 \n", + "4 Kentucky Farmers Bank Corporation October 25, 2019 \n", + ".. ... ... \n", + "556 Superior Federal, FSB July 27, 2001 \n", + "557 North Valley Bank May 3, 2001 \n", + "558 Southern New Hampshire Bank & Trust February 2, 2001 \n", + "559 Banterra Bank of Marion December 14, 2000 \n", + "560 Bank of the Orient October 13, 2000 \n", + "\n", + "[561 rows x 7 columns]" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sql_df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Py_for_DS_and_ML_Bootcamp/3_Pandas/README.md b/Py_for_DS_and_ML_Bootcamp/3_Pandas/README.md new file mode 100644 index 0000000..7e1fa22 --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/3_Pandas/README.md @@ -0,0 +1,14 @@ +# Introduction to Pandas + +You can think of pandas as an extremely powerful version of Excel, with a lot more features. + +In this section, you should go through the notebooks in this order: + +* Introduction to Pandas +* Series +* DataFrames +* Missing Data +* GroupBy +* Merging,Joining,and Concatenating +* Operations +* Data Input and Output diff --git a/Py_for_DS_and_ML_Bootcamp/README.md b/Py_for_DS_and_ML_Bootcamp/README.md new file mode 100644 index 0000000..a16b48b --- /dev/null +++ b/Py_for_DS_and_ML_Bootcamp/README.md @@ -0,0 +1,47 @@ +# Python for data science and machine learning Bootcamp + +[x] Python Crash Course + +[x] Python for Data Analysis - NumPy + +[] Python for Data Analysis - Pandas + +[] Python for Data Analysis - Pandas Exercises + +[] Python for Data Visualization - Matplotlib + +[] Python for Data Visualization - Seaborn + +[] Python for Data Visualization - Pandas Built-in Data Visualization + +[] Python for Data Visualization - Plotly and Cufflinks + +[] Python for Data Visualization - Geographical Plotting + +[] Data Capstone Project + +[] Introduction to Machine Learning + +[] Linear Regression + +[] Cross Validation and Bias-Variance Trade-Off + +[] Logistic Regression + +[] K Nearest Neighbors + +[] Decision Trees and Random Forests + +[] Support Vector Machines + +[] K Means Clustering + +[] Principal Component Analysis + +[] Recommender Systems + +[] Natural Language Processing + +[] Big Data and Spark with Python + +[] Neural Nets and Deep Learning