{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bb0ffed8",
   "metadata": {
    "id": "bb0ffed8"
   },
   "source": [
    "# Exercise Session 1 – Jupyter Notebook tutorial\n",
    "### ENV–501 Material and Energy Flow Analysis\n",
    "\n",
    "September 12, 2024\n",
    "\n",
    "\n",
    "#### Content of this tutorial:\n",
    "\n",
    "1. [Introduction to Jupyter notebook](#notebook)\n",
    "2. [An informal introduction to Python](#python)\n",
    "3. [Data handling with Pandas](#pandas)\n",
    "\n",
    "#### Sources:\n",
    "- Jupyter Notebook documentation: https://jupyter-notebook.readthedocs.io/en/stable/notebook.html\n",
    "- Jupyter Notebook tutorial: https://www.dataquest.io/blog/jupyter-notebook-tutorial/\n",
    "- Basic Python tutorial: https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator\n",
    "- Pandas tutorial: https://www.w3schools.com/python/pandas/default.asp\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53340197",
   "metadata": {
    "id": "53340197"
   },
   "source": [
    "<a id='notebook'></a>\n",
    "## Introduction to Jupyter Notebook\n",
    "\n",
    "### What is a “notebook”?\n",
    "\n",
    "A notebook integrates code and its output into a single document that combines visualizations, narrative text, mathematical equations, and other rich media.\n",
    "\n",
    "In other words: it’s a single document where you can run code, display the output, and also add explanations, formulas, charts, and make your work more transparent, understandable, repeatable, and shareable.\n",
    "\n",
    "### Kernel and Cell\n",
    "There are two main concepts that should be clarified:\n",
    "\n",
    "- A **kernel** is a “computational engine” that executes the code contained in a notebook document.\n",
    "- A **cell** is a container for text to be displayed in the notebook or code to be executed by the notebook’s kernel.\n",
    "\n",
    "### Types of cells\n",
    "There are two main cell types that we will use:\n",
    "\n",
    "- A **code cell** contains code to be executed in the kernel. When the code is run, the notebook displays the output below the code cell that generated it.\n",
    "- A **Markdown cell** contains text formatted using Markdown and displays its output in-place when the Markdown cell is run.\n",
    "\n",
    "Markdown is a lightweight, easy to learn markup language for formatting plain text.\n",
    "\n",
    "###  Keyboard shortcuts\n",
    "\n",
    "In a Jupyter Notebook, there is always one “active” cell highlighted with a border whose color denotes its current mode:\n",
    "\n",
    "- Green outline — cell is in “edit mode” and you can type into the cells like a normal text editor\n",
    "- Blue outline — cell is in “command mode” and you can edit the notebook as a whole, but not type into individual cells\n",
    "\n",
    "You can change between edit and command mode with Enter and Esc, respectively.\n",
    "Below some shortcuts in command mode:\n",
    "- Basic navigation: k (move up), j (move down)\n",
    "- Saving the notebook: s\n",
    "- Change Cell types: y (code cell), m (markdown cell)\n",
    "- Cell creation: a (add cell above), b (add cell below)\n",
    "- Cell editing: x (remove cell), c (copy cell), v (cell paste), z (undo delete cell)\n",
    "- Kernel operations: 0 (press twice for restart kernel)\n",
    "\n",
    "Below shortcuts in edit mode:\n",
    "- run cell: shift-enter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "658b967d",
   "metadata": {
    "id": "658b967d"
   },
   "source": [
    "<a id='python'></a>\n",
    "## An informal introduction to Python"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77975990",
   "metadata": {
    "id": "77975990"
   },
   "source": [
    "### Numbers\n",
    "\n",
    "The interpreter acts as a simple calculator: you can type an expression at it and it will write the value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a489c80d",
   "metadata": {
    "id": "a489c80d",
    "outputId": "371b0f38-fd08-4cfc-f0b7-7e6a43527f9f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6168f9fe",
   "metadata": {
    "id": "6168f9fe",
    "outputId": "a5bec6cb-e385-4c75-8ab3-4a3b3aa990c6"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum: 9\n",
      "product: 14\n",
      "exponential: 49\n",
      "division: 3.5\n",
      "floor division: 3\n",
      "modulus: 1\n"
     ]
    }
   ],
   "source": [
    "# define some variables and perform some operations\n",
    "a = 7\n",
    "b = 2\n",
    "\n",
    "print('sum:', a + b)\n",
    "print('product:', a * b)\n",
    "print('exponential:', a ** b)\n",
    "print('division:', a / b)\n",
    "print('floor division:', a // b)\n",
    "print('modulus:', a % b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fca6dcc2",
   "metadata": {
    "id": "fca6dcc2"
   },
   "source": [
    "### Strings\n",
    "\n",
    "Besides numbers, Python can also manipulate strings, which can be expressed as \"...\" or '...'."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "64b919da",
   "metadata": {
    "id": "64b919da"
   },
   "outputs": [],
   "source": [
    "# define your string\n",
    "mystring = \"My first string\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f3353f81",
   "metadata": {
    "id": "f3353f81",
    "outputId": "bf9c2e8c-6a13-4438-f656-bf18236fa76e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n"
     ]
    }
   ],
   "source": [
    "# string indexing\n",
    "print(mystring[4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8c2b9bf5",
   "metadata": {
    "id": "8c2b9bf5",
    "outputId": "6c37c600-c074-467c-ed72-7c18792cedb0"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My\n"
     ]
    }
   ],
   "source": [
    "# string slicing\n",
    "print(mystring[0:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a231fb8",
   "metadata": {
    "id": "5a231fb8"
   },
   "source": [
    "### Lists\n",
    "\n",
    "Python knows a number of compound data types, used to group together other values.\n",
    "\n",
    "The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.\n",
    "\n",
    "Python lists are 0-indexed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d8df34e5",
   "metadata": {
    "id": "d8df34e5",
    "outputId": "f5705393-c06e-44e8-917f-69534f048311"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 2, 1]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# define your list\n",
    "mylist = [1,2,3,4,2,1]\n",
    "mylist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "64878219",
   "metadata": {
    "id": "64878219",
    "outputId": "a57fa808-fe51-4499-f706-f26c21f03221"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "# list indexing\n",
    "print(mylist[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "081ccfc3",
   "metadata": {
    "id": "081ccfc3",
    "outputId": "3acc8034-3ef8-4cd2-ba45-b9fb09d79658"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "# list slicing\n",
    "print(mylist[2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "bafbba00",
   "metadata": {
    "id": "bafbba00",
    "outputId": "ddc14862-03d9-4a3c-ab34-1b09f8dafe7a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 0, 3, 4, 2, 1]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# change an element of a list\n",
    "mylist[1] = 0\n",
    "mylist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d77432df",
   "metadata": {
    "id": "d77432df",
    "outputId": "5470109e-52be-4299-eebb-c9169a62c529"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 0, 2]\n"
     ]
    }
   ],
   "source": [
    "# join or concatenate two lists\n",
    "list1 = [1,4,9]\n",
    "list2 = [0,2]\n",
    "\n",
    "list3 = list1 + list2\n",
    "print(list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e7023943",
   "metadata": {
    "id": "e7023943",
    "outputId": "e69f99f3-0d39-41ef-e1b3-3662765e9327"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 18]\n"
     ]
    }
   ],
   "source": [
    "# append an element to a list\n",
    "list1 = [1,4,9]\n",
    "\n",
    "list1.append(18)\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8410b8b5",
   "metadata": {
    "id": "8410b8b5"
   },
   "source": [
    "### Sets, tuples and dictionaries\n",
    "\n",
    "Together with list there are other 3 built-in data types in Python used to store collections of data.\n",
    "\n",
    "- A **sets** is a collection which is unordered, unchangeable, and unindexed.\n",
    "- A **tuple** is a collection which is ordered and unchangeable.\n",
    "- A **dictionary** is used to store data values in key:value pairs. It is ordered, changeable and do not allow duplicates.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f4019955",
   "metadata": {
    "id": "f4019955",
    "outputId": "4476b0f1-9e10-4ec7-b99c-a3e627baf08b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# set\n",
    "myset = {1,2,3}\n",
    "myset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "915f4cc6",
   "metadata": {
    "id": "915f4cc6",
    "outputId": "2572d986-6a76-459a-b7a1-88343a7bdd24"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tuple\n",
    "mytuple = (1,2,3)\n",
    "mytuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "cdf53639",
   "metadata": {
    "id": "cdf53639",
    "outputId": "d45d1a60-1eaa-4f6d-a10a-cc47565e05dd"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2: 4, 'key2': 3, 'key3': 10}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dictionary\n",
    "mydict = {2:4, 'key2':3, 'key3':10}\n",
    "mydict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "bc432317",
   "metadata": {
    "id": "bc432317",
    "outputId": "67063eba-f04c-463f-b03d-a064348b089a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# call an element of a dictionary using the key\n",
    "mydict['key2']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44c1998a",
   "metadata": {
    "id": "44c1998a"
   },
   "source": [
    "<a id='pandas'></a>\n",
    "## Pandas\n",
    "\n",
    "Pandas is a Python library used for working with datasets.\n",
    "\n",
    "It has functions for analyzing, cleaning, exploring, and manipulating data.\n",
    "\n",
    "Source tutorial: https://www.w3schools.com/python/pandas/default.asp\n",
    "\n",
    "if you have not installed pandas yet, you should enterthe command “pip install pandas” on the terminal or \"conda install pandas\" if you are using Anaconda Navigator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3efa87df",
   "metadata": {
    "id": "3efa87df"
   },
   "outputs": [],
   "source": [
    "# import the library\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "cec6a86a",
   "metadata": {
    "id": "cec6a86a",
    "outputId": "969c7e21-bcff-44bf-feb3-ed543eba02d9"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.985487</td>\n",
       "      <td>0.314422</td>\n",
       "      <td>-0.619870</td>\n",
       "      <td>-1.810922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.515624</td>\n",
       "      <td>-1.061167</td>\n",
       "      <td>-0.516456</td>\n",
       "      <td>-0.255987</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.688606</td>\n",
       "      <td>-1.568390</td>\n",
       "      <td>0.526393</td>\n",
       "      <td>1.242514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.011228</td>\n",
       "      <td>0.650628</td>\n",
       "      <td>1.642922</td>\n",
       "      <td>-0.251697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.776371</td>\n",
       "      <td>-0.432107</td>\n",
       "      <td>0.686684</td>\n",
       "      <td>0.418252</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D\n",
       "0 -0.985487  0.314422 -0.619870 -1.810922\n",
       "1 -0.515624 -1.061167 -0.516456 -0.255987\n",
       "2  1.688606 -1.568390  0.526393  1.242514\n",
       "3 -1.011228  0.650628  1.642922 -0.251697\n",
       "4  1.776371 -0.432107  0.686684  0.418252"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create sample dataset\n",
    "import pandas as pd\n",
    "import pandas._testing as tm\n",
    "import numpy as np\n",
    "\n",
    "df = pd.DataFrame(\n",
    "    np.random.randn(5, 4),\n",
    "    columns=list(\"ABCD\")\n",
    ")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "2620f682",
   "metadata": {
    "id": "2620f682",
    "outputId": "52d01c58-90e3-48e9-be1a-b32e8a7e7336"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 4)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# show dataframe shape\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "3bc02896",
   "metadata": {
    "id": "3bc02896",
    "outputId": "98217c3b-4d15-486d-c1a6-fc44d10c3ec9"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    float64\n",
       "B    float64\n",
       "C    float64\n",
       "D    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# column data types\n",
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "47b431c1",
   "metadata": {
    "id": "47b431c1",
    "outputId": "4a0c9ac3-a490-49a2-82bd-f807f0d89e37"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.190527</td>\n",
       "      <td>-0.419323</td>\n",
       "      <td>0.343935</td>\n",
       "      <td>-0.131568</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>1.421708</td>\n",
       "      <td>0.924075</td>\n",
       "      <td>0.936393</td>\n",
       "      <td>1.122558</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-1.011228</td>\n",
       "      <td>-1.568390</td>\n",
       "      <td>-0.619870</td>\n",
       "      <td>-1.810922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-0.985487</td>\n",
       "      <td>-1.061167</td>\n",
       "      <td>-0.516456</td>\n",
       "      <td>-0.255987</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>-0.515624</td>\n",
       "      <td>-0.432107</td>\n",
       "      <td>0.526393</td>\n",
       "      <td>-0.251697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.688606</td>\n",
       "      <td>0.314422</td>\n",
       "      <td>0.686684</td>\n",
       "      <td>0.418252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.776371</td>\n",
       "      <td>0.650628</td>\n",
       "      <td>1.642922</td>\n",
       "      <td>1.242514</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              A         B         C         D\n",
       "count  5.000000  5.000000  5.000000  5.000000\n",
       "mean   0.190527 -0.419323  0.343935 -0.131568\n",
       "std    1.421708  0.924075  0.936393  1.122558\n",
       "min   -1.011228 -1.568390 -0.619870 -1.810922\n",
       "25%   -0.985487 -1.061167 -0.516456 -0.255987\n",
       "50%   -0.515624 -0.432107  0.526393 -0.251697\n",
       "75%    1.688606  0.314422  0.686684  0.418252\n",
       "max    1.776371  0.650628  1.642922  1.242514"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# show basic dataframe info\n",
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "1df8563f",
   "metadata": {
    "id": "1df8563f",
    "outputId": "3865282f-7888-40cb-d398-3b82c50afbcc"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RangeIndex(start=0, stop=5, step=1)\n"
     ]
    }
   ],
   "source": [
    "# print dataframe index\n",
    "print(df.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "dbe8a4c4",
   "metadata": {
    "id": "dbe8a4c4",
    "outputId": "72d37ed0-6da1-45fc-8b90-d06097aabe9b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['A', 'B', 'C', 'D'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "# print dataframe columns\n",
    "print(df.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "e2481607",
   "metadata": {
    "id": "e2481607",
    "outputId": "609780f4-8677-43b3-d115-f797eda28007"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>COL1</th>\n",
       "      <th>COL2</th>\n",
       "      <th>COL3</th>\n",
       "      <th>COL4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>I1</th>\n",
       "      <td>-0.985487</td>\n",
       "      <td>0.314422</td>\n",
       "      <td>-0.619870</td>\n",
       "      <td>-1.810922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I2</th>\n",
       "      <td>-0.515624</td>\n",
       "      <td>-1.061167</td>\n",
       "      <td>-0.516456</td>\n",
       "      <td>-0.255987</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I3</th>\n",
       "      <td>1.688606</td>\n",
       "      <td>-1.568390</td>\n",
       "      <td>0.526393</td>\n",
       "      <td>1.242514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I4</th>\n",
       "      <td>-1.011228</td>\n",
       "      <td>0.650628</td>\n",
       "      <td>1.642922</td>\n",
       "      <td>-0.251697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I5</th>\n",
       "      <td>1.776371</td>\n",
       "      <td>-0.432107</td>\n",
       "      <td>0.686684</td>\n",
       "      <td>0.418252</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        COL1      COL2      COL3      COL4\n",
       "I1 -0.985487  0.314422 -0.619870 -1.810922\n",
       "I2 -0.515624 -1.061167 -0.516456 -0.255987\n",
       "I3  1.688606 -1.568390  0.526393  1.242514\n",
       "I4 -1.011228  0.650628  1.642922 -0.251697\n",
       "I5  1.776371 -0.432107  0.686684  0.418252"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# change name to the index or columns\n",
    "df.index = ['I1', 'I2', 'I3', 'I4', 'I5']\n",
    "df.columns = ['COL1', 'COL2', 'COL3', 'COL4']\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "2e5e2c1e",
   "metadata": {
    "id": "2e5e2c1e",
    "outputId": "c24ed0c7-2cf1-450d-dd61-8ac2f9d13a9a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.516455611291013\n",
      "-1.810921821312494\n"
     ]
    }
   ],
   "source": [
    "# A DataFrame is like a table with rows and columns.\n",
    "# Pandas use the loc attribute to return one or more specified elements of a dataframe\n",
    "print(df.loc['I2','COL3'])\n",
    "\n",
    "# otherwise use iloc for indexing using the corresponsng row and column numbers\n",
    "print(df.iloc[0,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "f4e7c663",
   "metadata": {
    "id": "f4e7c663",
    "outputId": "327c38ee-c11f-42a8-9dff-f26b7b8127d9"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I1   -0.985487\n",
      "I2   -0.515624\n",
      "I3    1.688606\n",
      "I4   -1.011228\n",
      "I5    1.776371\n",
      "Name: COL1, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# you can also call an entire column\n",
    "print(df.loc[:,'COL1'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "133bbb43",
   "metadata": {
    "id": "133bbb43",
    "outputId": "ce8bef44-06f8-4b79-e6dd-84d8c34f2867"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>COL1</th>\n",
       "      <th>COL2</th>\n",
       "      <th>COL3</th>\n",
       "      <th>COL4</th>\n",
       "      <th>COL5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>I1</th>\n",
       "      <td>-0.985487</td>\n",
       "      <td>0.314422</td>\n",
       "      <td>-0.619870</td>\n",
       "      <td>-1.810922</td>\n",
       "      <td>-0.671064</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I2</th>\n",
       "      <td>-0.515624</td>\n",
       "      <td>-1.061167</td>\n",
       "      <td>-0.516456</td>\n",
       "      <td>-0.255987</td>\n",
       "      <td>-1.576791</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I3</th>\n",
       "      <td>1.688606</td>\n",
       "      <td>-1.568390</td>\n",
       "      <td>0.526393</td>\n",
       "      <td>1.242514</td>\n",
       "      <td>0.120215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I4</th>\n",
       "      <td>-1.011228</td>\n",
       "      <td>0.650628</td>\n",
       "      <td>1.642922</td>\n",
       "      <td>-0.251697</td>\n",
       "      <td>-0.360600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I5</th>\n",
       "      <td>1.776371</td>\n",
       "      <td>-0.432107</td>\n",
       "      <td>0.686684</td>\n",
       "      <td>0.418252</td>\n",
       "      <td>1.344264</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        COL1      COL2      COL3      COL4      COL5\n",
       "I1 -0.985487  0.314422 -0.619870 -1.810922 -0.671064\n",
       "I2 -0.515624 -1.061167 -0.516456 -0.255987 -1.576791\n",
       "I3  1.688606 -1.568390  0.526393  1.242514  0.120215\n",
       "I4 -1.011228  0.650628  1.642922 -0.251697 -0.360600\n",
       "I5  1.776371 -0.432107  0.686684  0.418252  1.344264"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create a new column combining other columns\n",
    "df.loc[:,'COL5'] = df.loc[:,'COL1'] + df.loc[:,'COL2']\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "af0f056b",
   "metadata": {
    "id": "af0f056b",
    "outputId": "aefb5b40-7c3e-4b85-fbbb-298b71f53443"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Tom</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>nick</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>krish</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>jack</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Name  Age\n",
       "0    Tom   20\n",
       "1   nick   21\n",
       "2  krish   19\n",
       "3   jack   18"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dataframes can be also initialized starting from dictionaries\n",
    "data = {'Name': ['Tom', 'nick', 'krish', 'jack'],\n",
    "        'Age':  [20, 21, 19, 18],\n",
    "       }\n",
    "\n",
    "# create DataFrame from a dictionary\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "# print the output\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "146de052",
   "metadata": {
    "id": "146de052",
    "outputId": "0f71374a-c4cd-4637-fa93-e3c63089b83f"
   },
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'data_tutorial.xlsx'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[93], line 4\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# simple problem: find the color of the box with the largest volume\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \n\u001b[0;32m      3\u001b[0m \u001b[38;5;66;03m# load dataframe from an excel file\u001b[39;00m\n\u001b[1;32m----> 4\u001b[0m df_boxes \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mread_excel(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdata_tutorial.xlsx\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      5\u001b[0m df_boxes\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pandas\\io\\excel\\_base.py:495\u001b[0m, in \u001b[0;36mread_excel\u001b[1;34m(io, sheet_name, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, date_format, thousands, decimal, comment, skipfooter, storage_options, dtype_backend, engine_kwargs)\u001b[0m\n\u001b[0;32m    493\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(io, ExcelFile):\n\u001b[0;32m    494\u001b[0m     should_close \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m--> 495\u001b[0m     io \u001b[38;5;241m=\u001b[39m ExcelFile(\n\u001b[0;32m    496\u001b[0m         io,\n\u001b[0;32m    497\u001b[0m         storage_options\u001b[38;5;241m=\u001b[39mstorage_options,\n\u001b[0;32m    498\u001b[0m         engine\u001b[38;5;241m=\u001b[39mengine,\n\u001b[0;32m    499\u001b[0m         engine_kwargs\u001b[38;5;241m=\u001b[39mengine_kwargs,\n\u001b[0;32m    500\u001b[0m     )\n\u001b[0;32m    501\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m engine \u001b[38;5;129;01mand\u001b[39;00m engine \u001b[38;5;241m!=\u001b[39m io\u001b[38;5;241m.\u001b[39mengine:\n\u001b[0;32m    502\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    503\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEngine should not be specified when passing \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    504\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124man ExcelFile - ExcelFile already has the engine set\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    505\u001b[0m     )\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pandas\\io\\excel\\_base.py:1550\u001b[0m, in \u001b[0;36mExcelFile.__init__\u001b[1;34m(self, path_or_buffer, engine, storage_options, engine_kwargs)\u001b[0m\n\u001b[0;32m   1548\u001b[0m     ext \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mxls\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m   1549\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m-> 1550\u001b[0m     ext \u001b[38;5;241m=\u001b[39m inspect_excel_format(\n\u001b[0;32m   1551\u001b[0m         content_or_path\u001b[38;5;241m=\u001b[39mpath_or_buffer, storage_options\u001b[38;5;241m=\u001b[39mstorage_options\n\u001b[0;32m   1552\u001b[0m     )\n\u001b[0;32m   1553\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m ext \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m   1554\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m   1555\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mExcel file format cannot be determined, you must specify \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m   1556\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124man engine manually.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m   1557\u001b[0m         )\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pandas\\io\\excel\\_base.py:1402\u001b[0m, in \u001b[0;36minspect_excel_format\u001b[1;34m(content_or_path, storage_options)\u001b[0m\n\u001b[0;32m   1399\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(content_or_path, \u001b[38;5;28mbytes\u001b[39m):\n\u001b[0;32m   1400\u001b[0m     content_or_path \u001b[38;5;241m=\u001b[39m BytesIO(content_or_path)\n\u001b[1;32m-> 1402\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m get_handle(\n\u001b[0;32m   1403\u001b[0m     content_or_path, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrb\u001b[39m\u001b[38;5;124m\"\u001b[39m, storage_options\u001b[38;5;241m=\u001b[39mstorage_options, is_text\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m   1404\u001b[0m ) \u001b[38;5;28;01mas\u001b[39;00m handle:\n\u001b[0;32m   1405\u001b[0m     stream \u001b[38;5;241m=\u001b[39m handle\u001b[38;5;241m.\u001b[39mhandle\n\u001b[0;32m   1406\u001b[0m     stream\u001b[38;5;241m.\u001b[39mseek(\u001b[38;5;241m0\u001b[39m)\n",
      "File \u001b[1;32m~\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pandas\\io\\common.py:882\u001b[0m, in \u001b[0;36mget_handle\u001b[1;34m(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)\u001b[0m\n\u001b[0;32m    873\u001b[0m         handle \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(\n\u001b[0;32m    874\u001b[0m             handle,\n\u001b[0;32m    875\u001b[0m             ioargs\u001b[38;5;241m.\u001b[39mmode,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    878\u001b[0m             newline\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m    879\u001b[0m         )\n\u001b[0;32m    880\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    881\u001b[0m         \u001b[38;5;66;03m# Binary mode\u001b[39;00m\n\u001b[1;32m--> 882\u001b[0m         handle \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(handle, ioargs\u001b[38;5;241m.\u001b[39mmode)\n\u001b[0;32m    883\u001b[0m     handles\u001b[38;5;241m.\u001b[39mappend(handle)\n\u001b[0;32m    885\u001b[0m \u001b[38;5;66;03m# Convert BytesIO or file objects passed with an encoding\u001b[39;00m\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'data_tutorial.xlsx'"
     ]
    }
   ],
   "source": [
    "# simple problem: find the color of the box with the largest volume\n",
    "\n",
    "# load dataframe from an excel file\n",
    "df_boxes = pd.read_excel('data_tutorial.xlsx')\n",
    "df_boxes\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ef521de",
   "metadata": {
    "id": "9ef521de",
    "outputId": "fec0c36f-45a1-4325-d022-b56c35ffd120"
   },
   "outputs": [],
   "source": [
    "# calculate the volume of the boxes using the width, length and height columns\n",
    "df_boxes.loc[:,'volume'] = df_boxes.loc[:,'width'] * df_boxes.loc[:,'length'] * df_boxes.loc[:,'height']\n",
    "df_boxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4923028",
   "metadata": {
    "id": "a4923028",
    "outputId": "88bb831b-aeaa-4c25-d448-90df0fcb72a1"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the blue box has the largest volume\n"
     ]
    }
   ],
   "source": [
    "# index of the box with highest volumne\n",
    "index_max_vol = df_boxes.loc[:,'volume'].idxmax()\n",
    "\n",
    "# pick the corresponding color\n",
    "color = df_boxes.loc[index_max_vol,'color']\n",
    "print(f'the {color} box has the largest volume')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72eb3d0d",
   "metadata": {
    "id": "72eb3d0d"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "553b128b",
   "metadata": {
    "id": "553b128b"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
