{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EErgikq5jsjk",
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# Introduction\n",
    "\n",
    "**Environment preparation**\n",
    "\n",
    "* Create Python environment\n",
    "```\n",
    "python -m venv env-name\n",
    "```\n",
    "* Run venv\n",
    "```\n",
    "env-name\\Scripts\\Activate\n",
    "```\n",
    "* Intall python packages\n",
    "```\n",
    "pip install jupyter jupyterlab pandas matplotlib requests seaborn scipy scikit-learn\n",
    "```\n",
    "* Run Jupyter lab\n",
    "```\n",
    "jupyter lab\n",
    "```\n",
    "\n",
    "**Jupyter lab**\n",
    "\n",
    "* Add code\n",
    "* Add text\n",
    "* Execute command\n",
    "* Shortcuts (a, b, dd, Ctrl+Enter, Shift+Enter, x, c, v)\n",
    "\n",
    "\n",
    "\n",
    "**Alternatives**\n",
    "\n",
    "* Google Colab ([Colaboratory](https://colab.research.google.com/))\n",
    "* Python scripts in VS Code\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "l9hHDMhFjshW",
    "tags": []
   },
   "source": [
    "# Data processing\n",
    "\n",
    "Data source: https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data\n",
    "\n",
    "Important attributes description:\n",
    "* SalePrice: The property's sale price in dollars. This is the target variable that you're trying to predict.\n",
    "* MSSubClass: The building class\n",
    "* BldgType: Type of dwelling\n",
    "* HouseStyle: Style of dwelling\n",
    "* OverallQual: Overall material and finish quality\n",
    "* OverallCond: Overall condition rating\n",
    "* YearBuilt: Original construction date\n",
    "* Heating: Type of heating\n",
    "* CentralAir: Central air conditioning\n",
    "* GrLivArea: Above grade (ground) living area square feet\n",
    "* BedroomAbvGr: Number of bedrooms above basement level\n",
    "\n",
    "## Pandas tasks:\n",
    "* Load data\n",
    "* Standard data inspection (functions head(), tail(), describe(), isna(), shape)\n",
    "* Select one attribute to variable \n",
    "    - Series and numpy compatibility\n",
    "* dtype, index, columns\n",
    "* Data selection - [], loc, iloc\n",
    "* Data filtering and logical operators\n",
    "* Add new column to dataframe\n",
    "* Calculate new numberical attribute\n",
    "* Data selection - comparison and negation\n",
    "* Assign new values to selected rows from dataframe\n",
    "* Use .apply() for rows and single column\n",
    "* Use .groupby() for data aggregation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Import used packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "FVFwXLhL75FZ"
   },
   "outputs": [],
   "source": [
    "import pandas as pd # dataframes\n",
    "import numpy as np # matrices and linear algebra\n",
    "import matplotlib.pyplot as plt # plotting\n",
    "import seaborn as sns # another matplotlib interface - styled and easier to use"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The first step is to load the data into the Pandas DataFrame - in our case it is a csv file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## We shloud take a look on the data after loading so we know that everything is OK"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### We will start with showing first/last N rows \n",
    "- There are several ways of doing that:\n",
    "    - name of the dataframe\n",
    "    - head()\n",
    "    - tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Show 5 first and last rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Show first 5 rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Show last 20 rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## If we want to know if there are any missing values, the isna() function may render useful"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## We can show summary of common statistical characteristic of the data using the describe() function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Dataframe has several useful properties\n",
    "    - shape\n",
    "    - dtypes\n",
    "    - columns\n",
    "    - index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Row and column count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Datatypes of columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Column names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Row index values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## We may want to work with just one column not the whole dataframe\n",
    "- We will select only the SalePrice columns and save it to another variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Columns are called Pandas Series - it shares a common API with Pandas DataFrame\n",
    "- Pandas is numpy-backed so we can use Series as standard numpy arrays without any issues using the .values property"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## e.g. find maximum price using Numpy and Pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Data filtering using Pandas DataFrame\n",
    "- There are several ways of filtering the data (similar logic to .Where() in C# or WHERE in SQL)\n",
    "- We usually work with two indexers - .loc[] and .iloc[]\n",
    "\n",
    "### The .iloc[] indexer works with positional indexes - very close to the way of working with the raw arrays\n",
    "### The .loc[] indexer works with column names and logical expressions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Select all rows and 3rd column of dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Select all rows and LAST column of dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Select rows 15 to 22 and all columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Select rows 15 to 22 and 3rd column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Select only a subset of columns to a new dataframe\n",
    "- 'SalePrice','MSSubClass','BldgType','HouseStyle','OverallQual','OverallCond','YearBuilt','Heating','CentralAir','GrLivArea','BedroomAbvGr'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Select only houses built in year 2000 or later"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Select only houses that don't use GasA for heating (try != and ~ operators)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Select houses cheaper than 180k USD and with at least 2 bedrooms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Select houses with 2 stories or air conditioning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## We can add new columns to the DataFrame as well"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add a new column named Age for each house (current year - year built)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Add a new column IsLuxury with True value for houses with more than 3 bedrooms and price above 214k USD (.loc)\n",
    "- How many luxury houses are in the dataset?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### BONUS: We may apply function to each row of the dataset and compute the value that way as well"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Pandas enables us to use aggregation functions for the data using the .groupby() function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compute counts for all the heating methods (groupby / value_counts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "aAE33I3Tjse0",
    "tags": []
   },
   "source": [
    "# Visualization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "NcwzgU4oAQGF"
   },
   "source": [
    "## Scatter plot\n",
    "- Visualize relationship between SalePrice and GrLivArea.Use scatter plot from **Matplotlib**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 282
    },
    "id": "gHmSh07_ANdA",
    "outputId": "e98dab98-0c12-495b-b886-3a8afd17a2e0"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "i5snPTU2SUww"
   },
   "source": [
    "### Modify figure size and add title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 390
    },
    "id": "m-iXZtpDLZD8",
    "outputId": "d0a9c921-a48e-4fe1-c390-bc5bece63fc5"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "uh1KH9eRSeni"
   },
   "source": [
    "### Add axis labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 404
    },
    "id": "2-AYvW09Rwbh",
    "outputId": "09462606-d106-4797-f0ca-27d4776eaee9"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "IAoP4ndYSbEg"
   },
   "source": [
    "### Modify one of axis to logaritmic scale"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 404
    },
    "id": "WNa_f2wrOUTz",
    "outputId": "eb6f81fe-1fd1-4027-cea7-586e59464fc0"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "eQIMbATySmSj"
   },
   "source": [
    "### Add colors for data points based on CentralAir value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 404
    },
    "id": "kk7EzHSMSneC",
    "outputId": "3c3b9cfd-0d7b-4a71-c7aa-6bd4b82c484f"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ykYT_tiXRNAJ"
   },
   "source": [
    "### Try to use scatterplot from **Seaborn** library for scatter plot visualization."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Use series data for axes x and y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 296
    },
    "id": "2aZmNiIzOhw6",
    "outputId": "39cc7ecb-8a99-41a9-a611-f3a6b639989d"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use dataframe as source and column names for axes data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 296
    },
    "id": "ZrR0mwqoRK1_",
    "outputId": "f2e84a91-9700-4ae4-cc9e-eed5a54402b9"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Resize plot and add color for markers based on CentralAir column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 405
    },
    "id": "4rAYREWlRYRN",
    "outputId": "7ce5a397-a39d-47bd-9866-e48b351b452b"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Set color palette to *binary* scheme"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 388
    },
    "id": "3XHdtbgnUYnw",
    "outputId": "e539e551-2139-4f52-b7f0-2ceaec8b8f34"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "dT1O29MDVUEE"
   },
   "source": [
    "## Line plot\n",
    "- Calculate and vizualize average house price in relationship to YearBuild."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "M_pFtbhsVC2Q",
    "outputId": "147f9111-c2d6-46f0-ec38-ba0cb949e304"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 391
    },
    "id": "bfXtmkA6Whdm",
    "outputId": "c62905c8-18a0-4142-8445-dbcca60ed948"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### Create dataframe from the previous calculation and vizualize using Seaborn line plot (note: use .reset_index())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 419
    },
    "id": "6wg1gKl1Xq-h",
    "outputId": "094fc389-4d60-4bfd-eadb-ed9f8066e6c5"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 405
    },
    "id": "s2rRRBiFXcFL",
    "outputId": "25322264-28b6-4ed1-9f2b-9fce47ab3c6a"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "VH2TGgm7XL6i"
   },
   "source": [
    "## Bar plot\n",
    "- Calculate and vizualize how many of houses have CentralAir\n",
    "- Use Barplot for visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "N-luAVWBb1Yt",
    "outputId": "4994cd5b-fa7e-4fe8-91c1-18b6f323a386"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 282
    },
    "id": "2rKj7QEicmWX",
    "outputId": "8f970da3-5216-4a7e-b34f-3be812205569"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "SSkKDFEfb1Ql"
   },
   "source": [
    "### Vizualize number of building type and if they contains air conditioning using Seaborn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 483
    },
    "id": "RRRhPAvCc52Q",
    "outputId": "c158b93d-8bb2-4e4f-efc3-14c98f4f5bc3"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 296
    },
    "id": "pnpFAPXPb1HO",
    "outputId": "8708ceeb-005a-439b-ca0f-719e2203507e"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "m-iXZtpDLZD8",
    "tags": []
   },
   "source": [
    "# Tasks\n",
    "## Pandas (1 pt)\n",
    "Add a new column *Undervalued* which is set to True in case that the house is priced bellow 163k USD and has both OverallQual and OverallCond higher than 5.\n",
    "\n",
    "How many undervalued houses are in the dataset?\n",
    "\n",
    "## Vizualization (1 pt)\n",
    "Add to dataframe new attribute determining if the house was build before or after year 2000.\n",
    "\n",
    "Create bar chart for number of houses depending on type of dwelling (attribute BldgType, use as a category axis) and added binary attribute about house age (use as a bar color)."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "ZSU_cv1_introduction_jupyter_pandas_visualization.ipynb",
   "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.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
