{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![title](header.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Why Numpy and Pandas?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a long time, Python programmers who worked with data had to use the core Python libraries for importing, organising and manipulating data. The modules Numpy and Pandas give us tools we can use to work with data quickly and efficiently in a nicer format. By the end of this guide you should feel comfortable with Numpy arrays and Pandas series and dataframes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction to Numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy is a module that lets us generate more efficient lists that have the option to be multi-dimensional (stacked inside each other). Before we look at what Numpy can do, we have to first import the module. As with matplotlib, we will be importing it under a different name for brevity. Here we use \"np\"." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Almost all of Numpy's functionality comes from it's multi-dimensional arrays, which mostly operate like Python lists, but use less memory, have better performance and have some other cool features under the hood. To initialise an array, we use the function np.array:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4]\n" ] } ], "source": [ "a = np.array([1,2,3,4])\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A very important note is that the \"np.array\" function's argument is a list - Python won't understand you if you give it something else! For example - the command..." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "only 2 non-keyword arguments accepted", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0maError\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: only 2 non-keyword arguments accepted" ] } ], "source": [ "aError = np.array(1,2,3,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... give us an error because Python expects a list, not four numbers.\n", "\n", "We can also make two dimensional arrays like so (note the nested brackets):" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3 4]\n", " [5 6 7 8]]\n" ] } ], "source": [ "a = np.array([ [1,2,3,4] , [5,6,7,8] ])\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy also comes with some functions to generate arrays - for example the function linspace() gives us an array of numbers equally spread out between the arguements. For example:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5\n", " 7. 7.5 8. 8.5 9. 9.5 10. ]\n" ] } ], "source": [ "b = np.linspace(0,10,21)\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Gives us an array of 21 elements equally spaced from 0 to 10 (because we include endpoints here we have to be careful about how many numbers we want!)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy, unlike Python lists, can have operations performed on them directly:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.25 1. 2.25 4. 6.25 9. 12.25 16. 20.25\n", " 25. 30.25 36. 42.25 49. 56.25 64. 72.25 81. 90.25\n", " 100. ]\n" ] } ], "source": [ "print(b**2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use numpy arrays in the same way as Python lists for graphing (in fact this is recommended):" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt #We need to import our graphing library!\n", "\n", "plt.plot(b,b**2)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy arrays can also be indexed, sliced and iterated over in the same way as Python lists can:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0\n" ] } ], "source": [ "print(b[0])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. ]\n" ] } ], "source": [ "print(b[0:11])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0 is an element of our array!\n", "0.5 is an element of our array!\n", "1.0 is an element of our array!\n", "1.5 is an element of our array!\n", "2.0 is an element of our array!\n", "2.5 is an element of our array!\n", "3.0 is an element of our array!\n", "3.5 is an element of our array!\n", "4.0 is an element of our array!\n", "4.5 is an element of our array!\n", "5.0 is an element of our array!\n", "5.5 is an element of our array!\n", "6.0 is an element of our array!\n", "6.5 is an element of our array!\n", "7.0 is an element of our array!\n", "7.5 is an element of our array!\n", "8.0 is an element of our array!\n", "8.5 is an element of our array!\n", "9.0 is an element of our array!\n", "9.5 is an element of our array!\n", "10.0 is an element of our array!\n" ] } ], "source": [ "for element in b:\n", " print(str(element) + \" is an element of our array!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Apart from direct operations on arrays - this may seem a little redundant. So why do we use Numpy arrays over Python lists? Well, most scientific libraries leverage numpy for better performance. Better performance and a smaller memory footprint might not sound impressive now but imagine if you would work on a dataset with billions of entries; then these things become very important!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy is the backbone of most data focused Python libraries, because it provides a solid foundation to build upon. One of the most important of these libraries is Pandas, which we use to create series and dataframes, i.e tables.\n", "\n", "As always, we first need to import the library. With pandas we use the alias \"pd\" by convention:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Pandas' functionality is two Python objects - the series, and the dataframe. For making series, we can use the Series function (note: this is case sensitive!):" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 1\n", "1 1\n", "2 2\n", "3 3\n", "4 5\n", "5 8\n", "dtype: int64\n" ] } ], "source": [ "c = pd.Series([1,1,2,3,5,8])\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The numbers in the left column are our index - it can be helpful to change this, for example if our data is time based. To do this, we add another arguement to the series function:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Monday 1\n", "Tuesday 1\n", "Wednesday 2\n", "Thursday 3\n", "Friday 5\n", "Saturday 8\n", "dtype: int64\n" ] } ], "source": [ "c = pd.Series([1,1,2,3,5,8], index=[\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"])\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dataframes are just a collection of series. To make a dataframe, we have a few options, all using the DataFrame function (again notice the capitals!).\n", "\n", "We can pass a two dimensional numpy array as an arguement, along with column names (here we use the random sublibrary of numpy to give us a 6x4 array of random numbers):\n", "\n", "(Note: here np.random.randn(x,y) gives us an x by y numpy array with random entries)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " A B C D\n", "0 -1.304458 0.128916 0.917312 -0.830153\n", "1 -2.312278 -0.631357 -0.524241 -1.663592\n", "2 -1.527491 -0.136707 -1.041927 -0.649112\n", "3 0.621613 0.384033 -0.529702 1.046160\n", "4 0.603338 -1.430888 -1.414519 0.308135\n", "5 1.281230 -0.966232 0.618242 1.862749\n" ] } ], "source": [ "d = pd.DataFrame(np.random.randn(6,4), columns=['A','B','C','D'])\n", "print(d) #Note, if you are using jupyter notebook, just outputting d here instead of printing it will give you a nicer format." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another way is to pass a dictionary as our arguement to the function - here we use the function \"pd.date_range\" to generate an array of dates, starting from 30/03/2017 and ending at 02/04/2017:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " A B C\n", "0 1 hello 2017-03-30\n", "1 2 hello 2017-03-31\n", "2 3 hello 2017-04-01\n", "3 4 hello 2017-04-02\n" ] } ], "source": [ "e = pd.DataFrame({'A': [1,2,3,4], 'B':\"hello\", 'C': pd.date_range('20170330', periods=4)})\n", "print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most of the time during data analysis, we will looking at tables that are much larger than just 4 or 5 rows. It can be helpful to know some commands to give us summary information about our data without bringing up the whole frame.\n", "\n", "The head and tail methods give us the first and last row(s) of the frame:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " A B C D\n", "0 -1.304458 0.128916 0.917312 -0.830153\n", "1 -2.312278 -0.631357 -0.524241 -1.663592\n", "2 -1.527491 -0.136707 -1.041927 -0.649112\n" ] } ], "source": [ "print(d.head(3)) #Looks at the first 3 rows of our \"d\" dataframe" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " A B C\n", "3 4 hello 2017-04-02\n" ] } ], "source": [ "print(e.tail(1)) #Looks at the last row of our \"e\" dataframe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The describe method can give us some summary statistics of our data:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " A B C D\n", "count 6.000000 6.000000 6.000000 6.000000\n", "mean -0.439675 -0.442039 -0.329139 0.012365\n", "std 1.456953 0.691383 0.918213 1.306626\n", "min -2.312278 -1.430888 -1.414519 -1.663592\n", "25% -1.471733 -0.882513 -0.913870 -0.784893\n", "50% -0.350560 -0.384032 -0.526971 -0.170488\n", "75% 0.617044 0.062510 0.332621 0.861654\n", "max 1.281230 0.384033 0.917312 1.862749\n" ] } ], "source": [ "print(d.describe())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accessing columns and rows of a dataframe is similar to lists and arrays - for columns we index as normal:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 -1.304458\n", "1 -2.312278\n", "2 -1.527491\n", "3 0.621613\n", "4 0.603338\n", "5 1.281230\n", "Name: A, dtype: float64\n" ] } ], "source": [ "print(d['A'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For rows, we need to use the loc method:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A -1.304458\n", "B 0.128916\n", "C 0.917312\n", "D -0.830153\n", "Name: 0, dtype: float64\n" ] } ], "source": [ "print(d.loc[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And finally, to get induvidual values, we can use a double-index:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1.3044583932951868" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['A'][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we can filter our data using logical expressions, to access the row in our dataframe e where column A is equal to 3 we can use: " ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " A B C\n", "2 3 hello 2017-04-01\n" ] } ], "source": [ "print(e[e['A'] == 3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Worked Example" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "We're going to take a closer look at the random.randn function to see how this data is distributed:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0\n", "count 100000.000000\n", "mean -0.002194\n", "std 1.000691\n", "min -4.194139\n", "25% -0.673841\n", "50% 0.000094\n", "75% 0.671483\n", "max 4.440165\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "myData = pd.DataFrame(np.random.randn(100000))\n", "\n", "print(myData.describe())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So our random data has a mean of about 0 and a standard deviation of around 1. This seems to be a standard normal distribution, and in fact that's true - the \"n\" of \"randn\" stands for normal. We can illustrate this using a graph:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAD8CAYAAABzTgP2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3Xl8FfW9//HXJxtL2EMIEJaAoIgW8BpQRK0KWrVU9FdrqY/iWtFW29rlV9t629vW31LbXu291Z9IK2qr1doqFSuKUHetssm+hhAghCUQloQQspzP748cMIkngJDJnJPzfj4ePM6ZmW/mvD0meWdmzsyYuyMiInJYStgBREQkvqgYRESkERWDiIg0omIQEZFGVAwiItKIikFERBpRMYiISCMqBhERaUTFICIijaSFHeBE9OzZ0/Py8sKOISKSUBYtWrTL3bOPNS4hiyEvL4+FCxeGHUNEJKG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wAPBZdy8NO0+YzCyN+gPw44GtwALg+ui9zpOa1f9F9SRQ5u53h50nHkW3GL7v7hPDztIcHWOQ4/UQ0BmYa2ZLzGxa2IHCEj0Ifxcwh/qDq8+pFI4YB0wBLol+nyyJ/pUsCURbDCIi0oi2GEREpBEVg4iINKJiEBGRRlQMIiLSiIpBREQaUTGIiEgjKgYREWlExSAiIo38f+7wqiqfxtVHAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "\n", "mySortedData = myData.sort_values(0) #sorts the data in ascending order\n", "x = np.linspace(-10, 10, 100000) #setting up a dummy array\n", "\n", "plt.plot(mySortedData,x)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we can see the cumulative distribution function of the normal distribution!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mini Project" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below we have a dataframe of marks in a class - can you find out:\n", "\n", "* The average mark for English?\n", "* Each student's average mark? (DON'T do this manually!!!)\n", "* The subject which, on average, students scored the least marks in?\n", "* The subject which each induvidual student did worst in.\n", "* The student who got the most marks in the class overall" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "subjects = [\"Maths\",\"English\",\"Science\",\"Geography\",\"History\",\"Languages\"]\n", "marks = pd.DataFrame({\"Alice\": [85, 86, 98, 94, 2, 39],\"Billy\": [55, 26, 69, 39, 47, 15],\"Cameron\": [80, 5, 28, 28, 44, 37],\"David\": [ 5, 22, 95, 71, 62, 6],\"Ellie\": [75, 93, 66, 18, 87, 60],\"Faye\": [72, 0, 63, 51, 65, 83],\"Garry\": [67, 92, 62, 35, 0, 79],\"Harriet\": [51, 17, 87, 31, 91, 99],\"Izzy\": [63, 37, 58, 26, 39, 51],\"James\": [17, 7, 88, 27, 6, 16],\"Katie\": [15, 77, 12, 54, 81, 0],\"Liam\": [25, 35, 80, 71, 71, 9],\"Mason\": [70, 78, 4, 19, 61, 77],\"Noah\": [78, 96, 86, 42, 73, 51],\"Olivia\": [75, 81, 23, 19, 76, 3],\"Patrick\": [43, 50, 87, 94, 33, 65],\"Quinn\": [72, 1, 80, 96, 76, 56],\"Ross\": [ 3, 25, 30, 49, 84, 7],\"Sam\": [67, 29, 91, 64, 11, 43],\"Terri\": [63, 36, 70, 73, 13, 25],\"Umar\": [70, 30, 47, 71, 25, 57],\"Veronica\": [88, 34, 29, 92, 82, 62],\"Will\": [89, 11, 14, 56, 78, 63]}, index=subjects)" ] } ], "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }