Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save grahamhindle/c7814921d03a92d2867a3725fa6fb4c7 to your computer and use it in GitHub Desktop.
Save grahamhindle/c7814921d03a92d2867a3725fa6fb4c7 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"https://cocl.us/DA0101EN_edx_link_Notebook_link_top\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/Images/TopAd.png\" width=\"750\" align=\"center\">\n",
" </a>\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://www.bigdatauniversity.com\"><img src = \"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/Images/CCLog.png\" width = 300, align = \"center\"></a>\n",
"\n",
"<h1 align=center><font size=5>Data Analysis with Python</font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Data Wrangling</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Welcome!</h3>\n",
"\n",
"By the end of this notebook, you will have learned the basics of Data Wrangling! "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Table of content</h2>\n",
"\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"<ul>\n",
" <li><a href=\"#identify_handle_missing_values\">Identify and handle missing values</a>\n",
" <ul>\n",
" <li><a href=\"#identify_missing_values\">Identify missing values</a></li>\n",
" <li><a href=\"#deal_missing_values\">Deal with missing values</a></li>\n",
" <li><a href=\"#correct_data_format\">Correct data format</a></li>\n",
" </ul>\n",
" </li>\n",
" <li><a href=\"#data_standardization\">Data standardization</a></li>\n",
" <li><a href=\"#data_normalization\">Data Normalization (centering/scaling)</a></li>\n",
" <li><a href=\"#binning\">Binning</a></li>\n",
" <li><a href=\"#indicator\">Indicator variable</a></li>\n",
"</ul>\n",
" \n",
"Estimated Time Needed: <strong>30 min</strong>\n",
"</div>\n",
" \n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>What is the purpose of Data Wrangling?</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Data Wrangling is the process of converting data from the initial format to a format that may be better for analysis."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>What is the fuel consumption (L/100k) rate for the diesel car?</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Import data</h3>\n",
"<p>\n",
"You can find the \"Automobile Data Set\" from the following link: <a href=\"https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data\">https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data</a>. \n",
"We will be using this data set throughout this course.\n",
"</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Import pandas</h4> "
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import matplotlib.pylab as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Reading the data set from the URL and adding the related headers.</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"URL of the dataset"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This dataset was hosted on IBM Cloud object click <a href=\"https://cocl.us/edx_DA0101EN_objectstorage\">HERE</a> for free storage."
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [],
"source": [
"filename = \"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/auto.csv\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Python list <b>headers</b> containing name of headers "
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [],
"source": [
"headers = [\"symboling\",\"normalized-losses\",\"make\",\"fuel-type\",\"aspiration\", \"num-of-doors\",\"body-style\",\n",
" \"drive-wheels\",\"engine-location\",\"wheel-base\", \"length\",\"width\",\"height\",\"curb-weight\",\"engine-type\",\n",
" \"num-of-cylinders\", \"engine-size\",\"fuel-system\",\"bore\",\"stroke\",\"compression-ratio\",\"horsepower\",\n",
" \"peak-rpm\",\"city-mpg\",\"highway-mpg\",\"price\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the Pandas method <b>read_csv()</b> to load the data from the web address. Set the parameter \"names\" equal to the Python list \"headers\"."
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"df = pd.read_csv(filename, names = headers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Use the method <b>head()</b> to display the first five rows of the dataframe. "
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>symboling</th>\n",
" <th>normalized-losses</th>\n",
" <th>make</th>\n",
" <th>fuel-type</th>\n",
" <th>aspiration</th>\n",
" <th>num-of-doors</th>\n",
" <th>body-style</th>\n",
" <th>drive-wheels</th>\n",
" <th>engine-location</th>\n",
" <th>wheel-base</th>\n",
" <th>...</th>\n",
" <th>engine-size</th>\n",
" <th>fuel-system</th>\n",
" <th>bore</th>\n",
" <th>stroke</th>\n",
" <th>compression-ratio</th>\n",
" <th>horsepower</th>\n",
" <th>peak-rpm</th>\n",
" <th>city-mpg</th>\n",
" <th>highway-mpg</th>\n",
" <th>price</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3</td>\n",
" <td>?</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>130</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000</td>\n",
" <td>21</td>\n",
" <td>27</td>\n",
" <td>13495</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>?</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>130</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000</td>\n",
" <td>21</td>\n",
" <td>27</td>\n",
" <td>16500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>?</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>hatchback</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>94.5</td>\n",
" <td>...</td>\n",
" <td>152</td>\n",
" <td>mpfi</td>\n",
" <td>2.68</td>\n",
" <td>3.47</td>\n",
" <td>9.0</td>\n",
" <td>154</td>\n",
" <td>5000</td>\n",
" <td>19</td>\n",
" <td>26</td>\n",
" <td>16500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>fwd</td>\n",
" <td>front</td>\n",
" <td>99.8</td>\n",
" <td>...</td>\n",
" <td>109</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>10.0</td>\n",
" <td>102</td>\n",
" <td>5500</td>\n",
" <td>24</td>\n",
" <td>30</td>\n",
" <td>13950</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>4wd</td>\n",
" <td>front</td>\n",
" <td>99.4</td>\n",
" <td>...</td>\n",
" <td>136</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>8.0</td>\n",
" <td>115</td>\n",
" <td>5500</td>\n",
" <td>18</td>\n",
" <td>22</td>\n",
" <td>17450</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 26 columns</p>\n",
"</div>"
],
"text/plain": [
" symboling normalized-losses make fuel-type aspiration num-of-doors \\\n",
"0 3 ? alfa-romero gas std two \n",
"1 3 ? alfa-romero gas std two \n",
"2 1 ? alfa-romero gas std two \n",
"3 2 164 audi gas std four \n",
"4 2 164 audi gas std four \n",
"\n",
" body-style drive-wheels engine-location wheel-base ... engine-size \\\n",
"0 convertible rwd front 88.6 ... 130 \n",
"1 convertible rwd front 88.6 ... 130 \n",
"2 hatchback rwd front 94.5 ... 152 \n",
"3 sedan fwd front 99.8 ... 109 \n",
"4 sedan 4wd front 99.4 ... 136 \n",
"\n",
" fuel-system bore stroke compression-ratio horsepower peak-rpm city-mpg \\\n",
"0 mpfi 3.47 2.68 9.0 111 5000 21 \n",
"1 mpfi 3.47 2.68 9.0 111 5000 21 \n",
"2 mpfi 2.68 3.47 9.0 154 5000 19 \n",
"3 mpfi 3.19 3.40 10.0 102 5500 24 \n",
"4 mpfi 3.19 3.40 8.0 115 5500 18 \n",
"\n",
" highway-mpg price \n",
"0 27 13495 \n",
"1 27 16500 \n",
"2 26 16500 \n",
"3 30 13950 \n",
"4 22 17450 \n",
"\n",
"[5 rows x 26 columns]"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# To see what the data set looks like, we'll use the head() method.\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we can see, several question marks appeared in the dataframe; those are missing values which may hinder our further analysis. \n",
"<div>So, how do we identify all those missing values and deal with them?</div> \n",
"\n",
"\n",
"<b>How to work with missing data?</b>\n",
"\n",
"Steps for working with missing data:\n",
"<ol>\n",
" <li>dentify missing data</li>\n",
" <li>deal with missing data</li>\n",
" <li>correct data format</li>\n",
"</ol>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"identify_handle_missing_values\">Identify and handle missing values</h2>\n",
"\n",
"\n",
"<h3 id=\"identify_missing_values\">Identify missing values</h3>\n",
"<h4>Convert \"?\" to NaN</h4>\n",
"In the car dataset, missing data comes with the question mark \"?\".\n",
"We replace \"?\" with NaN (Not a Number), which is Python's default missing value marker, for reasons of computational speed and convenience. Here we use the function: \n",
" <pre>.replace(A, B, inplace = True) </pre>\n",
"to replace A by B"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>symboling</th>\n",
" <th>normalized-losses</th>\n",
" <th>make</th>\n",
" <th>fuel-type</th>\n",
" <th>aspiration</th>\n",
" <th>num-of-doors</th>\n",
" <th>body-style</th>\n",
" <th>drive-wheels</th>\n",
" <th>engine-location</th>\n",
" <th>wheel-base</th>\n",
" <th>...</th>\n",
" <th>engine-size</th>\n",
" <th>fuel-system</th>\n",
" <th>bore</th>\n",
" <th>stroke</th>\n",
" <th>compression-ratio</th>\n",
" <th>horsepower</th>\n",
" <th>peak-rpm</th>\n",
" <th>city-mpg</th>\n",
" <th>highway-mpg</th>\n",
" <th>price</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3</td>\n",
" <td>NaN</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>130</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000</td>\n",
" <td>21</td>\n",
" <td>27</td>\n",
" <td>13495</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>NaN</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>130</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000</td>\n",
" <td>21</td>\n",
" <td>27</td>\n",
" <td>16500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>NaN</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>hatchback</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>94.5</td>\n",
" <td>...</td>\n",
" <td>152</td>\n",
" <td>mpfi</td>\n",
" <td>2.68</td>\n",
" <td>3.47</td>\n",
" <td>9.0</td>\n",
" <td>154</td>\n",
" <td>5000</td>\n",
" <td>19</td>\n",
" <td>26</td>\n",
" <td>16500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>fwd</td>\n",
" <td>front</td>\n",
" <td>99.8</td>\n",
" <td>...</td>\n",
" <td>109</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>10.0</td>\n",
" <td>102</td>\n",
" <td>5500</td>\n",
" <td>24</td>\n",
" <td>30</td>\n",
" <td>13950</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>4wd</td>\n",
" <td>front</td>\n",
" <td>99.4</td>\n",
" <td>...</td>\n",
" <td>136</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>8.0</td>\n",
" <td>115</td>\n",
" <td>5500</td>\n",
" <td>18</td>\n",
" <td>22</td>\n",
" <td>17450</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 26 columns</p>\n",
"</div>"
],
"text/plain": [
" symboling normalized-losses make fuel-type aspiration num-of-doors \\\n",
"0 3 NaN alfa-romero gas std two \n",
"1 3 NaN alfa-romero gas std two \n",
"2 1 NaN alfa-romero gas std two \n",
"3 2 164 audi gas std four \n",
"4 2 164 audi gas std four \n",
"\n",
" body-style drive-wheels engine-location wheel-base ... engine-size \\\n",
"0 convertible rwd front 88.6 ... 130 \n",
"1 convertible rwd front 88.6 ... 130 \n",
"2 hatchback rwd front 94.5 ... 152 \n",
"3 sedan fwd front 99.8 ... 109 \n",
"4 sedan 4wd front 99.4 ... 136 \n",
"\n",
" fuel-system bore stroke compression-ratio horsepower peak-rpm city-mpg \\\n",
"0 mpfi 3.47 2.68 9.0 111 5000 21 \n",
"1 mpfi 3.47 2.68 9.0 111 5000 21 \n",
"2 mpfi 2.68 3.47 9.0 154 5000 19 \n",
"3 mpfi 3.19 3.40 10.0 102 5500 24 \n",
"4 mpfi 3.19 3.40 8.0 115 5500 18 \n",
"\n",
" highway-mpg price \n",
"0 27 13495 \n",
"1 27 16500 \n",
"2 26 16500 \n",
"3 30 13950 \n",
"4 22 17450 \n",
"\n",
"[5 rows x 26 columns]"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"# replace \"?\" to NaN\n",
"df.replace(\"?\", np.nan, inplace = True)\n",
"df.head(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"dentify_missing_values\n",
"\n",
"<h4>Evaluating for Missing Data</h4>\n",
"\n",
"The missing values are converted to Python's default. We use Python's built-in functions to identify these missing values. There are two methods to detect missing data:\n",
"<ol>\n",
" <li><b>.isnull()</b></li>\n",
" <li><b>.notnull()</b></li>\n",
"</ol>\n",
"The output is a boolean value indicating whether the value that is passed into the argument is in fact missing data."
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>symboling</th>\n",
" <th>normalized-losses</th>\n",
" <th>make</th>\n",
" <th>fuel-type</th>\n",
" <th>aspiration</th>\n",
" <th>num-of-doors</th>\n",
" <th>body-style</th>\n",
" <th>drive-wheels</th>\n",
" <th>engine-location</th>\n",
" <th>wheel-base</th>\n",
" <th>...</th>\n",
" <th>engine-size</th>\n",
" <th>fuel-system</th>\n",
" <th>bore</th>\n",
" <th>stroke</th>\n",
" <th>compression-ratio</th>\n",
" <th>horsepower</th>\n",
" <th>peak-rpm</th>\n",
" <th>city-mpg</th>\n",
" <th>highway-mpg</th>\n",
" <th>price</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 26 columns</p>\n",
"</div>"
],
"text/plain": [
" symboling normalized-losses make fuel-type aspiration num-of-doors \\\n",
"0 False True False False False False \n",
"1 False True False False False False \n",
"2 False True False False False False \n",
"3 False False False False False False \n",
"4 False False False False False False \n",
"\n",
" body-style drive-wheels engine-location wheel-base ... engine-size \\\n",
"0 False False False False ... False \n",
"1 False False False False ... False \n",
"2 False False False False ... False \n",
"3 False False False False ... False \n",
"4 False False False False ... False \n",
"\n",
" fuel-system bore stroke compression-ratio horsepower peak-rpm \\\n",
"0 False False False False False False \n",
"1 False False False False False False \n",
"2 False False False False False False \n",
"3 False False False False False False \n",
"4 False False False False False False \n",
"\n",
" city-mpg highway-mpg price \n",
"0 False False False \n",
"1 False False False \n",
"2 False False False \n",
"3 False False False \n",
"4 False False False \n",
"\n",
"[5 rows x 26 columns]"
]
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"missing_data = df.isnull()\n",
"missing_data.head(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\"True\" stands for missing value, while \"False\" stands for not missing value."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Count missing values in each column</h4>\n",
"<p>\n",
"Using a for loop in Python, we can quickly figure out the number of missing values in each column. As mentioned above, \"True\" represents a missing value, \"False\" means the value is present in the dataset. In the body of the for loop the method \".value_counts()\" counts the number of \"True\" values. \n",
"</p>"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"normalized-losses\n",
"False 164\n",
"True 41\n",
"Name: normalized-losses, dtype: int64\n",
"\n",
"num-of-doors\n",
"False 203\n",
"True 2\n",
"Name: num-of-doors, dtype: int64\n",
"\n",
"bore\n",
"False 201\n",
"True 4\n",
"Name: bore, dtype: int64\n",
"\n",
"stroke\n",
"False 201\n",
"True 4\n",
"Name: stroke, dtype: int64\n",
"\n",
"horsepower\n",
"False 203\n",
"True 2\n",
"Name: horsepower, dtype: int64\n",
"\n",
"peak-rpm\n",
"False 203\n",
"True 2\n",
"Name: peak-rpm, dtype: int64\n",
"\n",
"price\n",
"False 201\n",
"True 4\n",
"Name: price, dtype: int64\n",
"\n"
]
}
],
"source": [
"for column in missing_data.columns.values.tolist():\n",
" if (missing_data[column].any() == True):\n",
" print(column)\n",
" print (missing_data[column].value_counts())\n",
" print(\"\") "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Based on the summary above, each column has 205 rows of data, seven columns containing missing data:\n",
"<ol>\n",
" <li>\"normalized-losses\": 41 missing data</li>\n",
" <li>\"num-of-doors\": 2 missing data</li>\n",
" <li>\"bore\": 4 missing data</li>\n",
" <li>\"stroke\" : 4 missing data</li>\n",
" <li>\"horsepower\": 2 missing data</li>\n",
" <li>\"peak-rpm\": 2 missing data</li>\n",
" <li>\"price\": 4 missing data</li>\n",
"</ol>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"deal_missing_values\">Deal with missing data</h3>\n",
"<b>How to deal with missing data?</b>\n",
"\n",
"<ol>\n",
" <li>drop data<br>\n",
" a. drop the whole row<br>\n",
" b. drop the whole column\n",
" </li>\n",
" <li>replace data<br>\n",
" a. replace it by mean<br>\n",
" b. replace it by frequency<br>\n",
" c. replace it based on other functions\n",
" </li>\n",
"</ol>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Whole columns should be dropped only if most entries in the column are empty. In our dataset, none of the columns are empty enough to drop entirely.\n",
"We have some freedom in choosing which method to replace data; however, some methods may seem more reasonable than others. We will apply each method to many different columns:\n",
"\n",
"<b>Replace by mean:</b>\n",
"<ul>\n",
" <li>\"normalized-losses\": 41 missing data, replace them with mean</li>\n",
" <li>\"stroke\": 4 missing data, replace them with mean</li>\n",
" <li>\"bore\": 4 missing data, replace them with mean</li>\n",
" <li>\"horsepower\": 2 missing data, replace them with mean</li>\n",
" <li>\"peak-rpm\": 2 missing data, replace them with mean</li>\n",
"</ul>\n",
"\n",
"<b>Replace by frequency:</b>\n",
"<ul>\n",
" <li>\"num-of-doors\": 2 missing data, replace them with \"four\". \n",
" <ul>\n",
" <li>Reason: 84% sedans is four doors. Since four doors is most frequent, it is most likely to occur</li>\n",
" </ul>\n",
" </li>\n",
"</ul>\n",
"\n",
"<b>Drop the whole row:</b>\n",
"<ul>\n",
" <li>\"price\": 4 missing data, simply delete the whole row\n",
" <ul>\n",
" <li>Reason: price is what we want to predict. Any data entry without price data cannot be used for prediction; therefore any row now without price data is not useful to us</li>\n",
" </ul>\n",
" </li>\n",
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Calculate the average of the column </h4>"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Average of normalized-losses: 122.0\n"
]
}
],
"source": [
"avg_norm_loss = df[\"normalized-losses\"].astype(\"float\").mean(axis=0)\n",
"print(\"Average of normalized-losses:\", avg_norm_loss)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Replace \"NaN\" by mean value in \"normalized-losses\" column</h4>"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [],
"source": [
"df[\"normalized-losses\"].replace(np.nan, avg_norm_loss, inplace=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Calculate the mean value for 'bore' column</h4>"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Average of bore: 3.3297512437810943\n"
]
}
],
"source": [
"avg_bore=df['bore'].astype('float').mean(axis=0)\n",
"print(\"Average of bore:\", avg_bore)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Replace NaN by mean value</h4>"
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [],
"source": [
"df[\"bore\"].replace(np.nan, avg_bore, inplace=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #1: </h1>\n",
"\n",
"<b>According to the example above, replace NaN in \"stroke\" column by mean.</b>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.255422885572139\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"avg_stroke = df['stroke'].astype('float').mean(axis = 0)\n",
"print(avg_stroke)\n",
"df['stroke'].replace(np.nan, avg_stroke, inplace = True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"# calculate the mean vaule for \"stroke\" column\n",
"avg_stroke = df[\"stroke\"].astype(\"float\").mean(axis = 0)\n",
"print(\"Average of stroke:\", avg_stroke)\n",
"\n",
"# replace NaN by mean value in \"stroke\" column\n",
"df[\"stroke\"].replace(np.nan, avg_stroke, inplace = True)\n",
"\n",
"-->\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Calculate the mean value for the 'horsepower' column:</h4>"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Average horsepower: 104.25615763546799\n"
]
}
],
"source": [
"avg_horsepower = df['horsepower'].astype('float').mean(axis=0)\n",
"print(\"Average horsepower:\", avg_horsepower)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Replace \"NaN\" by mean value:</h4>"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [],
"source": [
"df['horsepower'].replace(np.nan, avg_horsepower, inplace=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Calculate the mean value for 'peak-rpm' column:</h4>"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Average peak rpm: 5125.369458128079\n"
]
}
],
"source": [
"avg_peakrpm=df['peak-rpm'].astype('float').mean(axis=0)\n",
"print(\"Average peak rpm:\", avg_peakrpm)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Replace NaN by mean value:</h4>"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [],
"source": [
"df['peak-rpm'].replace(np.nan, avg_peakrpm, inplace=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To see which values are present in a particular column, we can use the \".value_counts()\" method:"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"four 114\n",
"two 89\n",
"Name: num-of-doors, dtype: int64"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['num-of-doors'].value_counts()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that four doors are the most common type. We can also use the \".idxmax()\" method to calculate for us the most common type automatically:"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'four'"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['num-of-doors'].value_counts().idxmax()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The replacement procedure is very similar to what we have seen previously"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"#replace the missing 'num-of-doors' values by the most frequent \n",
"df[\"num-of-doors\"].replace(np.nan, \"four\", inplace=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, let's drop all rows that do not have price data:"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [],
"source": [
"# simply drop whole row with NaN in \"price\" column\n",
"df.dropna(subset=[\"price\"], axis=0, inplace=True)\n",
"\n",
"# reset index, because we droped two rows\n",
"df.reset_index(drop=True, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>symboling</th>\n",
" <th>normalized-losses</th>\n",
" <th>make</th>\n",
" <th>fuel-type</th>\n",
" <th>aspiration</th>\n",
" <th>num-of-doors</th>\n",
" <th>body-style</th>\n",
" <th>drive-wheels</th>\n",
" <th>engine-location</th>\n",
" <th>wheel-base</th>\n",
" <th>...</th>\n",
" <th>engine-size</th>\n",
" <th>fuel-system</th>\n",
" <th>bore</th>\n",
" <th>stroke</th>\n",
" <th>compression-ratio</th>\n",
" <th>horsepower</th>\n",
" <th>peak-rpm</th>\n",
" <th>city-mpg</th>\n",
" <th>highway-mpg</th>\n",
" <th>price</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>130</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000</td>\n",
" <td>21</td>\n",
" <td>27</td>\n",
" <td>13495</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>130</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000</td>\n",
" <td>21</td>\n",
" <td>27</td>\n",
" <td>16500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>hatchback</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>94.5</td>\n",
" <td>...</td>\n",
" <td>152</td>\n",
" <td>mpfi</td>\n",
" <td>2.68</td>\n",
" <td>3.47</td>\n",
" <td>9.0</td>\n",
" <td>154</td>\n",
" <td>5000</td>\n",
" <td>19</td>\n",
" <td>26</td>\n",
" <td>16500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>fwd</td>\n",
" <td>front</td>\n",
" <td>99.8</td>\n",
" <td>...</td>\n",
" <td>109</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>10.0</td>\n",
" <td>102</td>\n",
" <td>5500</td>\n",
" <td>24</td>\n",
" <td>30</td>\n",
" <td>13950</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>4wd</td>\n",
" <td>front</td>\n",
" <td>99.4</td>\n",
" <td>...</td>\n",
" <td>136</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>8.0</td>\n",
" <td>115</td>\n",
" <td>5500</td>\n",
" <td>18</td>\n",
" <td>22</td>\n",
" <td>17450</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 26 columns</p>\n",
"</div>"
],
"text/plain": [
" symboling normalized-losses make fuel-type aspiration num-of-doors \\\n",
"0 3 122 alfa-romero gas std two \n",
"1 3 122 alfa-romero gas std two \n",
"2 1 122 alfa-romero gas std two \n",
"3 2 164 audi gas std four \n",
"4 2 164 audi gas std four \n",
"\n",
" body-style drive-wheels engine-location wheel-base ... engine-size \\\n",
"0 convertible rwd front 88.6 ... 130 \n",
"1 convertible rwd front 88.6 ... 130 \n",
"2 hatchback rwd front 94.5 ... 152 \n",
"3 sedan fwd front 99.8 ... 109 \n",
"4 sedan 4wd front 99.4 ... 136 \n",
"\n",
" fuel-system bore stroke compression-ratio horsepower peak-rpm city-mpg \\\n",
"0 mpfi 3.47 2.68 9.0 111 5000 21 \n",
"1 mpfi 3.47 2.68 9.0 111 5000 21 \n",
"2 mpfi 2.68 3.47 9.0 154 5000 19 \n",
"3 mpfi 3.19 3.40 10.0 102 5500 24 \n",
"4 mpfi 3.19 3.40 8.0 115 5500 18 \n",
"\n",
" highway-mpg price \n",
"0 27 13495 \n",
"1 27 16500 \n",
"2 26 16500 \n",
"3 30 13950 \n",
"4 22 17450 \n",
"\n",
"[5 rows x 26 columns]"
]
},
"execution_count": 139,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Good!</b> Now, we obtain the dataset with no missing values."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"correct_data_format\">Correct data format</h3>\n",
"<b>We are almost there!</b>\n",
"<p>The last step in data cleaning is checking and making sure that all data is in the correct format (int, float, text or other).</p>\n",
"\n",
"In Pandas, we use \n",
"<p><b>.dtype()</b> to check the data type</p>\n",
"<p><b>.astype()</b> to change the data type</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Lets list the data types for each column</h4>"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"symboling int64\n",
"normalized-losses object\n",
"make object\n",
"fuel-type object\n",
"aspiration object\n",
"num-of-doors object\n",
"body-style object\n",
"drive-wheels object\n",
"engine-location object\n",
"wheel-base float64\n",
"length float64\n",
"width float64\n",
"height float64\n",
"curb-weight int64\n",
"engine-type object\n",
"num-of-cylinders object\n",
"engine-size int64\n",
"fuel-system object\n",
"bore object\n",
"stroke object\n",
"compression-ratio float64\n",
"horsepower object\n",
"peak-rpm object\n",
"city-mpg int64\n",
"highway-mpg int64\n",
"price object\n",
"dtype: object"
]
},
"execution_count": 140,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>As we can see above, some columns are not of the correct data type. Numerical variables should have type 'float' or 'int', and variables with strings such as categories should have type 'object'. For example, 'bore' and 'stroke' variables are numerical values that describe the engines, so we should expect them to be of the type 'float' or 'int'; however, they are shown as type 'object'. We have to convert data types into a proper format for each column using the \"astype()\" method.</p> "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Convert data types to proper format</h4>"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"df[[\"bore\", \"stroke\"]] = df[[\"bore\", \"stroke\"]].astype(\"float\")\n",
"df[[\"normalized-losses\"]] = df[[\"normalized-losses\"]].astype(\"int\")\n",
"df[[\"price\"]] = df[[\"price\"]].astype(\"float\")\n",
"df[[\"peak-rpm\"]] = df[[\"peak-rpm\"]].astype(\"float\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>Let us list the columns after the conversion</h4>"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"symboling int64\n",
"normalized-losses int64\n",
"make object\n",
"fuel-type object\n",
"aspiration object\n",
"num-of-doors object\n",
"body-style object\n",
"drive-wheels object\n",
"engine-location object\n",
"wheel-base float64\n",
"length float64\n",
"width float64\n",
"height float64\n",
"curb-weight int64\n",
"engine-type object\n",
"num-of-cylinders object\n",
"engine-size int64\n",
"fuel-system object\n",
"bore float64\n",
"stroke float64\n",
"compression-ratio float64\n",
"horsepower object\n",
"peak-rpm float64\n",
"city-mpg int64\n",
"highway-mpg int64\n",
"price float64\n",
"dtype: object"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Wonderful!</b>\n",
"\n",
"Now, we finally obtain the cleaned dataset with no missing values and all data in its proper format."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"data_standardization\">Data Standardization</h2>\n",
"<p>\n",
"Data is usually collected from different agencies with different formats.\n",
"(Data Standardization is also a term for a particular type of data normalization, where we subtract the mean and divide by the standard deviation)\n",
"</p>\n",
" \n",
"<b>What is Standardization?</b>\n",
"<p>Standardization is the process of transforming data into a common format which allows the researcher to make the meaningful comparison.\n",
"</p>\n",
"\n",
"<b>Example</b>\n",
"<p>Transform mpg to L/100km:</p>\n",
"<p>In our dataset, the fuel consumption columns \"city-mpg\" and \"highway-mpg\" are represented by mpg (miles per gallon) unit. Assume we are developing an application in a country that accept the fuel consumption with L/100km standard</p>\n",
"<p>We will need to apply <b>data transformation</b> to transform mpg into L/100km?</p>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>The formula for unit conversion is<p>\n",
"L/100km = 235 / mpg\n",
"<p>We can do many mathematical operations directly in Pandas.</p>"
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>symboling</th>\n",
" <th>normalized-losses</th>\n",
" <th>make</th>\n",
" <th>fuel-type</th>\n",
" <th>aspiration</th>\n",
" <th>num-of-doors</th>\n",
" <th>body-style</th>\n",
" <th>drive-wheels</th>\n",
" <th>engine-location</th>\n",
" <th>wheel-base</th>\n",
" <th>...</th>\n",
" <th>engine-size</th>\n",
" <th>fuel-system</th>\n",
" <th>bore</th>\n",
" <th>stroke</th>\n",
" <th>compression-ratio</th>\n",
" <th>horsepower</th>\n",
" <th>peak-rpm</th>\n",
" <th>city-mpg</th>\n",
" <th>highway-mpg</th>\n",
" <th>price</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>130</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000.0</td>\n",
" <td>21</td>\n",
" <td>27</td>\n",
" <td>13495.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>130</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000.0</td>\n",
" <td>21</td>\n",
" <td>27</td>\n",
" <td>16500.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>hatchback</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>94.5</td>\n",
" <td>...</td>\n",
" <td>152</td>\n",
" <td>mpfi</td>\n",
" <td>2.68</td>\n",
" <td>3.47</td>\n",
" <td>9.0</td>\n",
" <td>154</td>\n",
" <td>5000.0</td>\n",
" <td>19</td>\n",
" <td>26</td>\n",
" <td>16500.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>fwd</td>\n",
" <td>front</td>\n",
" <td>99.8</td>\n",
" <td>...</td>\n",
" <td>109</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>10.0</td>\n",
" <td>102</td>\n",
" <td>5500.0</td>\n",
" <td>24</td>\n",
" <td>30</td>\n",
" <td>13950.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>4wd</td>\n",
" <td>front</td>\n",
" <td>99.4</td>\n",
" <td>...</td>\n",
" <td>136</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>8.0</td>\n",
" <td>115</td>\n",
" <td>5500.0</td>\n",
" <td>18</td>\n",
" <td>22</td>\n",
" <td>17450.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 26 columns</p>\n",
"</div>"
],
"text/plain": [
" symboling normalized-losses make fuel-type aspiration \\\n",
"0 3 122 alfa-romero gas std \n",
"1 3 122 alfa-romero gas std \n",
"2 1 122 alfa-romero gas std \n",
"3 2 164 audi gas std \n",
"4 2 164 audi gas std \n",
"\n",
" num-of-doors body-style drive-wheels engine-location wheel-base ... \\\n",
"0 two convertible rwd front 88.6 ... \n",
"1 two convertible rwd front 88.6 ... \n",
"2 two hatchback rwd front 94.5 ... \n",
"3 four sedan fwd front 99.8 ... \n",
"4 four sedan 4wd front 99.4 ... \n",
"\n",
" engine-size fuel-system bore stroke compression-ratio horsepower \\\n",
"0 130 mpfi 3.47 2.68 9.0 111 \n",
"1 130 mpfi 3.47 2.68 9.0 111 \n",
"2 152 mpfi 2.68 3.47 9.0 154 \n",
"3 109 mpfi 3.19 3.40 10.0 102 \n",
"4 136 mpfi 3.19 3.40 8.0 115 \n",
"\n",
" peak-rpm city-mpg highway-mpg price \n",
"0 5000.0 21 27 13495.0 \n",
"1 5000.0 21 27 16500.0 \n",
"2 5000.0 19 26 16500.0 \n",
"3 5500.0 24 30 13950.0 \n",
"4 5500.0 18 22 17450.0 \n",
"\n",
"[5 rows x 26 columns]"
]
},
"execution_count": 143,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>symboling</th>\n",
" <th>normalized-losses</th>\n",
" <th>make</th>\n",
" <th>fuel-type</th>\n",
" <th>aspiration</th>\n",
" <th>num-of-doors</th>\n",
" <th>body-style</th>\n",
" <th>drive-wheels</th>\n",
" <th>engine-location</th>\n",
" <th>wheel-base</th>\n",
" <th>...</th>\n",
" <th>fuel-system</th>\n",
" <th>bore</th>\n",
" <th>stroke</th>\n",
" <th>compression-ratio</th>\n",
" <th>horsepower</th>\n",
" <th>peak-rpm</th>\n",
" <th>city-mpg</th>\n",
" <th>highway-mpg</th>\n",
" <th>price</th>\n",
" <th>city-L/100km</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000.0</td>\n",
" <td>21</td>\n",
" <td>27</td>\n",
" <td>13495.0</td>\n",
" <td>11.190476</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000.0</td>\n",
" <td>21</td>\n",
" <td>27</td>\n",
" <td>16500.0</td>\n",
" <td>11.190476</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>hatchback</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>94.5</td>\n",
" <td>...</td>\n",
" <td>mpfi</td>\n",
" <td>2.68</td>\n",
" <td>3.47</td>\n",
" <td>9.0</td>\n",
" <td>154</td>\n",
" <td>5000.0</td>\n",
" <td>19</td>\n",
" <td>26</td>\n",
" <td>16500.0</td>\n",
" <td>12.368421</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>fwd</td>\n",
" <td>front</td>\n",
" <td>99.8</td>\n",
" <td>...</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>10.0</td>\n",
" <td>102</td>\n",
" <td>5500.0</td>\n",
" <td>24</td>\n",
" <td>30</td>\n",
" <td>13950.0</td>\n",
" <td>9.791667</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>4wd</td>\n",
" <td>front</td>\n",
" <td>99.4</td>\n",
" <td>...</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>8.0</td>\n",
" <td>115</td>\n",
" <td>5500.0</td>\n",
" <td>18</td>\n",
" <td>22</td>\n",
" <td>17450.0</td>\n",
" <td>13.055556</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 27 columns</p>\n",
"</div>"
],
"text/plain": [
" symboling normalized-losses make fuel-type aspiration \\\n",
"0 3 122 alfa-romero gas std \n",
"1 3 122 alfa-romero gas std \n",
"2 1 122 alfa-romero gas std \n",
"3 2 164 audi gas std \n",
"4 2 164 audi gas std \n",
"\n",
" num-of-doors body-style drive-wheels engine-location wheel-base ... \\\n",
"0 two convertible rwd front 88.6 ... \n",
"1 two convertible rwd front 88.6 ... \n",
"2 two hatchback rwd front 94.5 ... \n",
"3 four sedan fwd front 99.8 ... \n",
"4 four sedan 4wd front 99.4 ... \n",
"\n",
" fuel-system bore stroke compression-ratio horsepower peak-rpm city-mpg \\\n",
"0 mpfi 3.47 2.68 9.0 111 5000.0 21 \n",
"1 mpfi 3.47 2.68 9.0 111 5000.0 21 \n",
"2 mpfi 2.68 3.47 9.0 154 5000.0 19 \n",
"3 mpfi 3.19 3.40 10.0 102 5500.0 24 \n",
"4 mpfi 3.19 3.40 8.0 115 5500.0 18 \n",
"\n",
" highway-mpg price city-L/100km \n",
"0 27 13495.0 11.190476 \n",
"1 27 16500.0 11.190476 \n",
"2 26 16500.0 12.368421 \n",
"3 30 13950.0 9.791667 \n",
"4 22 17450.0 13.055556 \n",
"\n",
"[5 rows x 27 columns]"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Convert mpg to L/100km by mathematical operation (235 divided by mpg)\n",
"df['city-L/100km'] = 235/df[\"city-mpg\"]\n",
"\n",
"# check your transformed data \n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #2: </h1>\n",
"\n",
"<b>According to the example above, transform mpg to L/100km in the column of \"highway-mpg\", and change the name of column to \"highway-L/100km\".</b>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>symboling</th>\n",
" <th>normalized-losses</th>\n",
" <th>make</th>\n",
" <th>fuel-type</th>\n",
" <th>aspiration</th>\n",
" <th>num-of-doors</th>\n",
" <th>body-style</th>\n",
" <th>drive-wheels</th>\n",
" <th>engine-location</th>\n",
" <th>wheel-base</th>\n",
" <th>...</th>\n",
" <th>fuel-system</th>\n",
" <th>bore</th>\n",
" <th>stroke</th>\n",
" <th>compression-ratio</th>\n",
" <th>horsepower</th>\n",
" <th>peak-rpm</th>\n",
" <th>city-mpg</th>\n",
" <th>price</th>\n",
" <th>city-L/100km</th>\n",
" <th>highway-L/100km</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000.0</td>\n",
" <td>21</td>\n",
" <td>13495.0</td>\n",
" <td>11.190476</td>\n",
" <td>8.703704</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>...</td>\n",
" <td>mpfi</td>\n",
" <td>3.47</td>\n",
" <td>2.68</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000.0</td>\n",
" <td>21</td>\n",
" <td>16500.0</td>\n",
" <td>11.190476</td>\n",
" <td>8.703704</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>hatchback</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>94.5</td>\n",
" <td>...</td>\n",
" <td>mpfi</td>\n",
" <td>2.68</td>\n",
" <td>3.47</td>\n",
" <td>9.0</td>\n",
" <td>154</td>\n",
" <td>5000.0</td>\n",
" <td>19</td>\n",
" <td>16500.0</td>\n",
" <td>12.368421</td>\n",
" <td>9.038462</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>fwd</td>\n",
" <td>front</td>\n",
" <td>99.8</td>\n",
" <td>...</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>10.0</td>\n",
" <td>102</td>\n",
" <td>5500.0</td>\n",
" <td>24</td>\n",
" <td>13950.0</td>\n",
" <td>9.791667</td>\n",
" <td>7.833333</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>gas</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>4wd</td>\n",
" <td>front</td>\n",
" <td>99.4</td>\n",
" <td>...</td>\n",
" <td>mpfi</td>\n",
" <td>3.19</td>\n",
" <td>3.40</td>\n",
" <td>8.0</td>\n",
" <td>115</td>\n",
" <td>5500.0</td>\n",
" <td>18</td>\n",
" <td>17450.0</td>\n",
" <td>13.055556</td>\n",
" <td>10.681818</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 27 columns</p>\n",
"</div>"
],
"text/plain": [
" symboling normalized-losses make fuel-type aspiration \\\n",
"0 3 122 alfa-romero gas std \n",
"1 3 122 alfa-romero gas std \n",
"2 1 122 alfa-romero gas std \n",
"3 2 164 audi gas std \n",
"4 2 164 audi gas std \n",
"\n",
" num-of-doors body-style drive-wheels engine-location wheel-base ... \\\n",
"0 two convertible rwd front 88.6 ... \n",
"1 two convertible rwd front 88.6 ... \n",
"2 two hatchback rwd front 94.5 ... \n",
"3 four sedan fwd front 99.8 ... \n",
"4 four sedan 4wd front 99.4 ... \n",
"\n",
" fuel-system bore stroke compression-ratio horsepower peak-rpm city-mpg \\\n",
"0 mpfi 3.47 2.68 9.0 111 5000.0 21 \n",
"1 mpfi 3.47 2.68 9.0 111 5000.0 21 \n",
"2 mpfi 2.68 3.47 9.0 154 5000.0 19 \n",
"3 mpfi 3.19 3.40 10.0 102 5500.0 24 \n",
"4 mpfi 3.19 3.40 8.0 115 5500.0 18 \n",
"\n",
" price city-L/100km highway-L/100km \n",
"0 13495.0 11.190476 8.703704 \n",
"1 16500.0 11.190476 8.703704 \n",
"2 16500.0 12.368421 9.038462 \n",
"3 13950.0 9.791667 7.833333 \n",
"4 17450.0 13.055556 10.681818 \n",
"\n",
"[5 rows x 27 columns]"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"df['highway-L/100km'] = 235 / df['highway-mpg']\n",
"df.drop('highway-mpg',axis = 1, inplace = True)\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"# transform mpg to L/100km by mathematical operation (235 divided by mpg)\n",
"df[\"highway-mpg\"] = 235/df[\"highway-mpg\"]\n",
"\n",
"# rename column name from \"highway-mpg\" to \"highway-L/100km\"\n",
"df.rename(columns={'\"highway-mpg\"':'highway-L/100km'}, inplace=True)\n",
"\n",
"# check your transformed data \n",
"df.head()\n",
"\n",
"-->\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"data_normalization\">Data Normalization</h2>\n",
"\n",
"<b>Why normalization?</b>\n",
"<p>Normalization is the process of transforming values of several variables into a similar range. Typical normalizations include scaling the variable so the variable average is 0, scaling the variable so the variance is 1, or scaling variable so the variable values range from 0 to 1\n",
"</p>\n",
"\n",
"<b>Example</b>\n",
"<p>To demonstrate normalization, let's say we want to scale the columns \"length\", \"width\" and \"height\" </p>\n",
"<p><b>Target:</b>would like to Normalize those variables so their value ranges from 0 to 1.</p>\n",
"<p><b>Approach:</b> replace original value by (original value)/(maximum value)</p>"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"# replace (original value) by (original value)/(maximum value)\n",
"df['length'] = df['length']/df['length'].max()\n",
"df['width'] = df['width']/df['width'].max()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Questiont #3: </h1>\n",
"\n",
"<b>According to the example above, normalize the column \"height\".</b>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 147,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>length</th>\n",
" <th>width</th>\n",
" <th>height</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.811148</td>\n",
" <td>0.890278</td>\n",
" <td>0.816054</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.811148</td>\n",
" <td>0.890278</td>\n",
" <td>0.816054</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.822681</td>\n",
" <td>0.909722</td>\n",
" <td>0.876254</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.848630</td>\n",
" <td>0.919444</td>\n",
" <td>0.908027</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.848630</td>\n",
" <td>0.922222</td>\n",
" <td>0.908027</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" length width height\n",
"0 0.811148 0.890278 0.816054\n",
"1 0.811148 0.890278 0.816054\n",
"2 0.822681 0.909722 0.876254\n",
"3 0.848630 0.919444 0.908027\n",
"4 0.848630 0.922222 0.908027"
]
},
"execution_count": 147,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"df['height'] = df['height'] / df['height'].max()\n",
"df[['length', 'width', 'height']].head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"df['height'] = df['height']/df['height'].max() \n",
"# show the scaled columns\n",
"df[[\"length\",\"width\",\"height\"]].head()\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we can see, we've normalized \"length\", \"width\" and \"height\" in the range of [0,1]."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"binning\">Binning</h2>\n",
"<b>Why binning?</b>\n",
"<p>\n",
" Binning is a process of transforming continuous numerical variables into discrete categorical 'bins', for grouped analysis.\n",
"</p>\n",
"\n",
"<b>Example: </b>\n",
"<p>In our dataset, \"horsepower\" is a real valued variable ranging from 48 to 288, it has 57 unique values. What if we only care about the price difference between cars with high horsepower, medium horsepower, and little horsepower (3 types)? Can we rearrange them into three ‘bins' to simplify analysis? </p>\n",
"\n",
"<p>We will use the Pandas method 'cut' to segment the 'horsepower' column into 3 bins </p>\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example of Binning Data In Pandas</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Convert data to correct format "
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"df[\"horsepower\"]=df[\"horsepower\"].astype(int, copy=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets plot the histogram of horspower, to see what the distribution of horsepower looks like."
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(0.5, 1.0, 'horsepower bins')"
]
},
"execution_count": 149,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEWCAYAAABhffzLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAVC0lEQVR4nO3de7SldX3f8feHAUXlHgYcwThIiStoDeoEtRSkEBXBZqiJF1awU0OCdqnRVU0yJk1q2piCMSbGusrCahjjldQg09il0lEGSRQdDCKIBoKDF0ZmuChoWxT89o/nN2Uzc87hAPOcfeb83q+19trPdT/f/Zs9n/07z372b6eqkCT1Y49pFyBJWlgGvyR1xuCXpM4Y/JLUGYNfkjpj8EtSZwx+TVWSzUl+Ydp1LHZJTkzy7TnWn5fk9xayJu2+9px2AZIevqp61bRr0O7DHr+WhCRLphOzlJ6LFieDX4vBMUmuTvL9JB9Jsvf2FUl+PckNSW5Psj7J4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CMzx7VhPAp4CXDIMA8MyhqEkZqpTesgMfi1Fd09M3wscMNuGzQ+3T1TVq5I8EzgNuCrJMQzD6b62qj45uVOSE9l5WN1i5uF3t/sc8AqGsVo+y9CbfzbwBuCngWur6tkPVKf0cHiOXz34PnBHkuPb/MuBjTNtmOTIqrqiqn4fuJWhZ/5J4N+2UzEk+Zk26ivAsUmOaOf2XwpczvBjHM9JcnCSZcAZE8e7DHhju/974F8Ad1fV9xneDJYneXY7zl5JnrzrmkEa2ONXL9YA5yV5NHAjQ697Jn+c5CiGXvsGhhFerwZWAl9qQ/Fu476fGvwccA7wTxnC/KKq+kmSNwGfaY/zP6tq+7DFn2V4M7msqu5N8i3gawBV9aN2OunPk+zP8P/zzxhGmJV2GUfnlB6idqrnjVX1wmnXIj0YnuqRpM7Y45ekztjjl6TOGPyS1BmDX5I6Y/BLUmcMfknqzP8DYRcBLtUxGX8AAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"%matplotlib inline\n",
"import matplotlib as plt\n",
"from matplotlib import pyplot\n",
"plt.pyplot.hist(df[\"horsepower\"])\n",
"\n",
"# set x/y labels and plot title\n",
"plt.pyplot.xlabel(\"horsepower\")\n",
"plt.pyplot.ylabel(\"count\")\n",
"plt.pyplot.title(\"horsepower bins\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We would like 3 bins of equal size bandwidth so we use numpy's <code>linspace(start_value, end_value, numbers_generated</code> function.</p>\n",
"<p>Since we want to include the minimum value of horsepower we want to set start_value=min(df[\"horsepower\"]).</p>\n",
"<p>Since we want to include the maximum value of horsepower we want to set end_value=max(df[\"horsepower\"]).</p>\n",
"<p>Since we are building 3 bins of equal length, there should be 4 dividers, so numbers_generated=4.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We build a bin array, with a minimum value to a maximum value, with bandwidth calculated above. The bins will be values used to determine when one bin ends and another begins."
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 48. , 119.33333333, 190.66666667, 262. ])"
]
},
"execution_count": 150,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bins = np.linspace(min(df[\"horsepower\"]), max(df[\"horsepower\"]), 4)\n",
"bins"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We set group names:"
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [],
"source": [
"group_names = ['Low', 'Medium', 'High']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We apply the function \"cut\" the determine what each value of \"df['horsepower']\" belongs to. "
]
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>horsepower</th>\n",
" <th>horsepower-binned</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>111</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>111</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>154</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>102</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>115</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>110</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>110</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>110</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>140</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>101</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>101</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>121</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>121</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>121</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>182</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>182</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>182</td>\n",
" <td>Medium</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>48</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>70</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>70</td>\n",
" <td>Low</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" horsepower horsepower-binned\n",
"0 111 Low\n",
"1 111 Low\n",
"2 154 Medium\n",
"3 102 Low\n",
"4 115 Low\n",
"5 110 Low\n",
"6 110 Low\n",
"7 110 Low\n",
"8 140 Medium\n",
"9 101 Low\n",
"10 101 Low\n",
"11 121 Medium\n",
"12 121 Medium\n",
"13 121 Medium\n",
"14 182 Medium\n",
"15 182 Medium\n",
"16 182 Medium\n",
"17 48 Low\n",
"18 70 Low\n",
"19 70 Low"
]
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['horsepower-binned'] = pd.cut(df['horsepower'], bins, labels=group_names, include_lowest=True )\n",
"df[['horsepower','horsepower-binned']].head(20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets see the number of vehicles in each bin."
]
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Low 153\n",
"Medium 43\n",
"High 5\n",
"Name: horsepower-binned, dtype: int64"
]
},
"execution_count": 153,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"horsepower-binned\"].value_counts()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets plot the distribution of each bin."
]
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(0.5, 1.0, 'horsepower bins')"
]
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEWCAYAAACJ0YulAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAZFUlEQVR4nO3de7QlZX3m8e9Do1wEAeWASAONBJ0Aw6C2eBuQSFSijs3SqM0S0yIGcRHUiahNnIijkuBlYryG1YkI3oD2gjDRKKQNNEYBG0UQEOnh2oD0ARERletv/qjqYnM83X049N77dO/vZ629dtX7Vu36nbPX2c+pql1vpaqQJAlgo2EXIEmaOQwFSVLHUJAkdQwFSVLHUJAkdQwFSVLHUNCMlOS6JH867DpmuiQHJFmxhv4Tk/ztIGvS+m3jYRcgqX+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T5P8JMnSdplZ7WiaP2zXf3PbfkA72ukZSa5IcmKSjdq+Q5Jc1r7Wh9q21yT5h3b6bUmuaad3S/K9dvqZSc5LcnGS7yTZYbI6B/vr1IbI/yw0SnYHDqmqv0yyGHgVzdXHR1fVeUneDxwHvL1dfuuqegFAksuAl1TVTXnohjmHA3dW1bPaYSv+M8nZbd++NKPZXg98G3hlku8DH6IZQfUO4OwkB9MMkfHOdr39gNuT7Egz2OH5SR4DfBKYV1XjSV5LMxLrGyfWKT1ahoJGybVVdUk7fTGwG80H6nlt2ynAV3qWP71n+j+Bk9sw+Xrb9mJg7yR/3s5vRRM89wIXVdWq//hPpfmAvw84t6rG2/YvAftX1TeSbJFkS5rRUL9MM+rqfu22nkYzhPY57ZBLs4BbVlOn9KgYCholE0e6XNstMu9eNVFVRyZ5Ns3Ndy5Jsg/N+DhHV9V3eldKcgB/OGJqseaROH8AHAZcRTOe1Rtphk9/B83Q3Je34/yvsU7p0fKcgkbZncAdSfZr518PnDfZgkl2q6oLq+q9wG00/9F/B3hLe3iHJE9tb7gDsG+SXdtzCa+lGeX2QuAFae7CNovmfg2rtrcUOKZ9/jHNDXzuqao7aYJiLMlz2+08Jsme6+7XID3EPQWNugXAiUk2p7mZzWGrWe4jSXan+W9/CfATmhvpzAF+1A6lPQ4c3C7/A+AEmjt1LQXOqKoH27F1/qN9nW9V1Znt8ufTBM3SqnogyY20o6hW1b3tIapPJNmK5u/2H4HL19HvQOo49pG0jrWHj46pKu+XoPWOh48kSR33FCRJHfcUJEkdQ0GS1DEUJEkdQ0GS1DEUJEmd/w/TIY+Xunm9GQAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"%matplotlib inline\n",
"import matplotlib as plt\n",
"from matplotlib import pyplot\n",
"pyplot.bar(group_names, df[\"horsepower-binned\"].value_counts())\n",
"\n",
"# set x/y labels and plot title\n",
"plt.pyplot.xlabel(\"horsepower\")\n",
"plt.pyplot.ylabel(\"count\")\n",
"plt.pyplot.title(\"horsepower bins\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>\n",
" Check the dataframe above carefully, you will find the last column provides the bins for \"horsepower\" with 3 categories (\"Low\",\"Medium\" and \"High\"). \n",
"</p>\n",
"<p>\n",
" We successfully narrow the intervals from 57 to 3!\n",
"</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Bins visualization</h3>\n",
"Normally, a histogram is used to visualize the distribution of bins we created above. "
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"Text(0.5, 1.0, 'horsepower bins')"
]
},
"execution_count": 155,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEWCAYAAACJ0YulAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAYdklEQVR4nO3dfbRddX3n8feHoPgA8tBcEAkYZKLT4DiIKUodlUoVfBjD2KphFSeD2GgXos6IGupU7Ezp4EO11tG6YkFipWB8QDLLGYVGSbQVMCjyKJJCgEAklweFWhsBv/PH3tkc4r3JJeScc5Pzfq111tn7t/c++3t/OTmfs/c5+3dSVUiSBLDLsAuQJE0fhoIkqWMoSJI6hoIkqWMoSJI6hoIkqWMoaFpKsjbJ7w67jukuyVFJ1m1h+aeT/Mkga9KObddhFyCpf6rqrcOuQTsWjxS0U0uy07zx2Zn+Fk1fhoKms8OSXJXkZ0m+kOQJmxYk+cMka5Lck2R5kqf1LKskJye5EbgxjY8l2dA+1lVJnt2uu1uSjyS5Ncmd7emWJ7bLjkqyLskfJ7mrPaX1Bz372TPJ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vZy4DlJfr+d35MmeH4JXF5Vm97xn0fzAv8AcElVjbft5wIvrqqvJtm9HZPrQODvaH7g50Xtvp4FPBu4uBlWhxk0w29MVKf0mBgKGiUbe6YfAvaabMXWzzdNVNVbkzwfeBVwZZLDaIY8PqWqvtG7UZKj+PWhj4uJh0je5LvAiTRj33yb5ijgSOBdwEHAtVV15NbqlB4rP1PQKPsZcG+SF7XzbwRWTrRikkOq6rKqej9wF807+m8Af9Se3iHJM9uRdwGOSHJw+1nCG4Dv0PzQykuSzEwyAzi+Z3+rgFPb+x8AvwNsrKqf0QTFWJIj2/08Lsmh268bpId5pKBRtxD4dJInATfRvFufyIeTzKF5t7+CZpTdq4DZwPfb4ZLHefjnJ78LnAn8O5oX+guq6ldJTgO+1T7O/62qTUNLf5smaFZV1UNJbgN+BFBVv2xPUf1Vkj1p/t/+Jc0ov9J25Sip0nbWnj46tapePexapEfL00eSpI5HCpKkjkcKkqSOoSBJ6hgKkqSOoSBJ6hgKkqTO/wfvYYrxyMFQgQAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"%matplotlib inline\n",
"import matplotlib as plt\n",
"from matplotlib import pyplot\n",
"\n",
"a = (0,1,2)\n",
"\n",
"# draw historgram of attribute \"horsepower\" with bins = 3\n",
"plt.pyplot.hist(df[\"horsepower\"], bins = 3)\n",
"\n",
"# set x/y labels and plot title\n",
"plt.pyplot.xlabel(\"horsepower\")\n",
"plt.pyplot.ylabel(\"count\")\n",
"plt.pyplot.title(\"horsepower bins\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plot above shows the binning result for attribute \"horsepower\". "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"indicator\">Indicator variable (or dummy variable)</h2>\n",
"<b>What is an indicator variable?</b>\n",
"<p>\n",
" An indicator variable (or dummy variable) is a numerical variable used to label categories. They are called 'dummies' because the numbers themselves don't have inherent meaning. \n",
"</p>\n",
"\n",
"<b>Why we use indicator variables?</b>\n",
"<p>\n",
" So we can use categorical variables for regression analysis in the later modules.\n",
"</p>\n",
"<b>Example</b>\n",
"<p>\n",
" We see the column \"fuel-type\" has two unique values, \"gas\" or \"diesel\". Regression doesn't understand words, only numbers. To use this attribute in regression analysis, we convert \"fuel-type\" into indicator variables.\n",
"</p>\n",
"\n",
"<p>\n",
" We will use the panda's method 'get_dummies' to assign numerical values to different categories of fuel type. \n",
"</p>"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"Index(['symboling', 'normalized-losses', 'make', 'fuel-type', 'aspiration',\n",
" 'num-of-doors', 'body-style', 'drive-wheels', 'engine-location',\n",
" 'wheel-base', 'length', 'width', 'height', 'curb-weight', 'engine-type',\n",
" 'num-of-cylinders', 'engine-size', 'fuel-system', 'bore', 'stroke',\n",
" 'compression-ratio', 'horsepower', 'peak-rpm', 'city-mpg', 'price',\n",
" 'city-L/100km', 'highway-L/100km', 'horsepower-binned'],\n",
" dtype='object')"
]
},
"execution_count": 156,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.columns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"get indicator variables and assign it to data frame \"dummy_variable_1\" "
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>diesel</th>\n",
" <th>gas</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" diesel gas\n",
"0 0 1\n",
"1 0 1\n",
"2 0 1\n",
"3 0 1\n",
"4 0 1"
]
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dummy_variable_1 = pd.get_dummies(df[\"fuel-type\"])\n",
"dummy_variable_1.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"change column names for clarity "
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>fuel-type-diesel</th>\n",
" <th>fuel-type-gas</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" fuel-type-diesel fuel-type-gas\n",
"0 0 1\n",
"1 0 1\n",
"2 0 1\n",
"3 0 1\n",
"4 0 1"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dummy_variable_1.rename(columns={'gas':'fuel-type-gas', 'diesel':'fuel-type-diesel'}, inplace=True)\n",
"dummy_variable_1.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now have the value 0 to represent \"gas\" and 1 to represent \"diesel\" in the column \"fuel-type\". We will now insert this column back into our original dataset. "
]
},
{
"cell_type": "code",
"execution_count": 159,
"metadata": {},
"outputs": [],
"source": [
"# merge data frame \"df\" and \"dummy_variable_1\" \n",
"df = pd.concat([df, dummy_variable_1], axis=1)\n",
"\n",
"# drop original column \"fuel-type\" from \"df\"\n",
"df.drop(\"fuel-type\", axis = 1, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>symboling</th>\n",
" <th>normalized-losses</th>\n",
" <th>make</th>\n",
" <th>aspiration</th>\n",
" <th>num-of-doors</th>\n",
" <th>body-style</th>\n",
" <th>drive-wheels</th>\n",
" <th>engine-location</th>\n",
" <th>wheel-base</th>\n",
" <th>length</th>\n",
" <th>...</th>\n",
" <th>compression-ratio</th>\n",
" <th>horsepower</th>\n",
" <th>peak-rpm</th>\n",
" <th>city-mpg</th>\n",
" <th>price</th>\n",
" <th>city-L/100km</th>\n",
" <th>highway-L/100km</th>\n",
" <th>horsepower-binned</th>\n",
" <th>fuel-type-diesel</th>\n",
" <th>fuel-type-gas</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>0.811148</td>\n",
" <td>...</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000.0</td>\n",
" <td>21</td>\n",
" <td>13495.0</td>\n",
" <td>11.190476</td>\n",
" <td>8.703704</td>\n",
" <td>Low</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>0.811148</td>\n",
" <td>...</td>\n",
" <td>9.0</td>\n",
" <td>111</td>\n",
" <td>5000.0</td>\n",
" <td>21</td>\n",
" <td>16500.0</td>\n",
" <td>11.190476</td>\n",
" <td>8.703704</td>\n",
" <td>Low</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>std</td>\n",
" <td>two</td>\n",
" <td>hatchback</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>94.5</td>\n",
" <td>0.822681</td>\n",
" <td>...</td>\n",
" <td>9.0</td>\n",
" <td>154</td>\n",
" <td>5000.0</td>\n",
" <td>19</td>\n",
" <td>16500.0</td>\n",
" <td>12.368421</td>\n",
" <td>9.038462</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>fwd</td>\n",
" <td>front</td>\n",
" <td>99.8</td>\n",
" <td>0.848630</td>\n",
" <td>...</td>\n",
" <td>10.0</td>\n",
" <td>102</td>\n",
" <td>5500.0</td>\n",
" <td>24</td>\n",
" <td>13950.0</td>\n",
" <td>9.791667</td>\n",
" <td>7.833333</td>\n",
" <td>Low</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>std</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>4wd</td>\n",
" <td>front</td>\n",
" <td>99.4</td>\n",
" <td>0.848630</td>\n",
" <td>...</td>\n",
" <td>8.0</td>\n",
" <td>115</td>\n",
" <td>5500.0</td>\n",
" <td>18</td>\n",
" <td>17450.0</td>\n",
" <td>13.055556</td>\n",
" <td>10.681818</td>\n",
" <td>Low</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 29 columns</p>\n",
"</div>"
],
"text/plain": [
" symboling normalized-losses make aspiration num-of-doors \\\n",
"0 3 122 alfa-romero std two \n",
"1 3 122 alfa-romero std two \n",
"2 1 122 alfa-romero std two \n",
"3 2 164 audi std four \n",
"4 2 164 audi std four \n",
"\n",
" body-style drive-wheels engine-location wheel-base length ... \\\n",
"0 convertible rwd front 88.6 0.811148 ... \n",
"1 convertible rwd front 88.6 0.811148 ... \n",
"2 hatchback rwd front 94.5 0.822681 ... \n",
"3 sedan fwd front 99.8 0.848630 ... \n",
"4 sedan 4wd front 99.4 0.848630 ... \n",
"\n",
" compression-ratio horsepower peak-rpm city-mpg price city-L/100km \\\n",
"0 9.0 111 5000.0 21 13495.0 11.190476 \n",
"1 9.0 111 5000.0 21 16500.0 11.190476 \n",
"2 9.0 154 5000.0 19 16500.0 12.368421 \n",
"3 10.0 102 5500.0 24 13950.0 9.791667 \n",
"4 8.0 115 5500.0 18 17450.0 13.055556 \n",
"\n",
" highway-L/100km horsepower-binned fuel-type-diesel fuel-type-gas \n",
"0 8.703704 Low 0 1 \n",
"1 8.703704 Low 0 1 \n",
"2 9.038462 Medium 0 1 \n",
"3 7.833333 Low 0 1 \n",
"4 10.681818 Low 0 1 \n",
"\n",
"[5 rows x 29 columns]"
]
},
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The last two columns are now the indicator variable representation of the fuel-type variable. It's all 0s and 1s now."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #4: </h1>\n",
"\n",
"<b>As above, create indicator variable to the column of \"aspiration\": \"std\" to 0, while \"turbo\" to 1.</b>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>aspiration-std</th>\n",
" <th>aspiration-turbo</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" aspiration-std aspiration-turbo\n",
"0 1 0\n",
"1 1 0\n",
"2 1 0\n",
"3 1 0\n",
"4 1 0"
]
},
"execution_count": 161,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"dummy_variable2 = pd.get_dummies(df['aspiration'])\n",
"dummy_variable2.head()\n",
"dummy_variable2.rename(columns={'std':'aspiration-std', 'turbo':'aspiration-turbo'}, inplace=True)\n",
"dummy_variable2.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"# get indicator variables of aspiration and assign it to data frame \"dummy_variable_2\"\n",
"dummy_variable_2 = pd.get_dummies(df['aspiration'])\n",
"\n",
"# change column names for clarity\n",
"dummy_variable_2.rename(columns={'std':'aspiration-std', 'turbo': 'aspiration-turbo'}, inplace=True)\n",
"\n",
"# show first 5 instances of data frame \"dummy_variable_1\"\n",
"dummy_variable_2.head()\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <div class=\"alert alert-danger alertdanger\" style=\"margin-top: 20px\">\n",
"<h1> Question #5: </h1>\n",
"\n",
"<b>Merge the new dataframe to the original dataframe then drop the column 'aspiration'</b>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>symboling</th>\n",
" <th>normalized-losses</th>\n",
" <th>make</th>\n",
" <th>num-of-doors</th>\n",
" <th>body-style</th>\n",
" <th>drive-wheels</th>\n",
" <th>engine-location</th>\n",
" <th>wheel-base</th>\n",
" <th>length</th>\n",
" <th>width</th>\n",
" <th>...</th>\n",
" <th>peak-rpm</th>\n",
" <th>city-mpg</th>\n",
" <th>price</th>\n",
" <th>city-L/100km</th>\n",
" <th>highway-L/100km</th>\n",
" <th>horsepower-binned</th>\n",
" <th>fuel-type-diesel</th>\n",
" <th>fuel-type-gas</th>\n",
" <th>aspiration-std</th>\n",
" <th>aspiration-turbo</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>0.811148</td>\n",
" <td>0.890278</td>\n",
" <td>...</td>\n",
" <td>5000.0</td>\n",
" <td>21</td>\n",
" <td>13495.0</td>\n",
" <td>11.190476</td>\n",
" <td>8.703704</td>\n",
" <td>Low</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>two</td>\n",
" <td>convertible</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>88.6</td>\n",
" <td>0.811148</td>\n",
" <td>0.890278</td>\n",
" <td>...</td>\n",
" <td>5000.0</td>\n",
" <td>21</td>\n",
" <td>16500.0</td>\n",
" <td>11.190476</td>\n",
" <td>8.703704</td>\n",
" <td>Low</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>122</td>\n",
" <td>alfa-romero</td>\n",
" <td>two</td>\n",
" <td>hatchback</td>\n",
" <td>rwd</td>\n",
" <td>front</td>\n",
" <td>94.5</td>\n",
" <td>0.822681</td>\n",
" <td>0.909722</td>\n",
" <td>...</td>\n",
" <td>5000.0</td>\n",
" <td>19</td>\n",
" <td>16500.0</td>\n",
" <td>12.368421</td>\n",
" <td>9.038462</td>\n",
" <td>Medium</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>fwd</td>\n",
" <td>front</td>\n",
" <td>99.8</td>\n",
" <td>0.848630</td>\n",
" <td>0.919444</td>\n",
" <td>...</td>\n",
" <td>5500.0</td>\n",
" <td>24</td>\n",
" <td>13950.0</td>\n",
" <td>9.791667</td>\n",
" <td>7.833333</td>\n",
" <td>Low</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>164</td>\n",
" <td>audi</td>\n",
" <td>four</td>\n",
" <td>sedan</td>\n",
" <td>4wd</td>\n",
" <td>front</td>\n",
" <td>99.4</td>\n",
" <td>0.848630</td>\n",
" <td>0.922222</td>\n",
" <td>...</td>\n",
" <td>5500.0</td>\n",
" <td>18</td>\n",
" <td>17450.0</td>\n",
" <td>13.055556</td>\n",
" <td>10.681818</td>\n",
" <td>Low</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 30 columns</p>\n",
"</div>"
],
"text/plain": [
" symboling normalized-losses make num-of-doors body-style \\\n",
"0 3 122 alfa-romero two convertible \n",
"1 3 122 alfa-romero two convertible \n",
"2 1 122 alfa-romero two hatchback \n",
"3 2 164 audi four sedan \n",
"4 2 164 audi four sedan \n",
"\n",
" drive-wheels engine-location wheel-base length width ... peak-rpm \\\n",
"0 rwd front 88.6 0.811148 0.890278 ... 5000.0 \n",
"1 rwd front 88.6 0.811148 0.890278 ... 5000.0 \n",
"2 rwd front 94.5 0.822681 0.909722 ... 5000.0 \n",
"3 fwd front 99.8 0.848630 0.919444 ... 5500.0 \n",
"4 4wd front 99.4 0.848630 0.922222 ... 5500.0 \n",
"\n",
" city-mpg price city-L/100km highway-L/100km horsepower-binned \\\n",
"0 21 13495.0 11.190476 8.703704 Low \n",
"1 21 16500.0 11.190476 8.703704 Low \n",
"2 19 16500.0 12.368421 9.038462 Medium \n",
"3 24 13950.0 9.791667 7.833333 Low \n",
"4 18 17450.0 13.055556 10.681818 Low \n",
"\n",
" fuel-type-diesel fuel-type-gas aspiration-std aspiration-turbo \n",
"0 0 1 1 0 \n",
"1 0 1 1 0 \n",
"2 0 1 1 0 \n",
"3 0 1 1 0 \n",
"4 0 1 1 0 \n",
"\n",
"[5 rows x 30 columns]"
]
},
"execution_count": 162,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"df = pd.concat([df, dummy_variable2], axis = 1)\n",
"df.drop('aspiration', axis = 1, inplace=True)\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click <b>here</b> for the solution.\n",
"\n",
"<!-- The answer is below:\n",
"\n",
"#merge the new dataframe to the original datafram\n",
"df = pd.concat([df, dummy_variable_2], axis=1)\n",
"\n",
"# drop original column \"aspiration\" from \"df\"\n",
"df.drop('aspiration', axis = 1, inplace=True)\n",
"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"save the new csv "
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [],
"source": [
"df.to_csv('clean_df.csv')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Thank you for completing this notebook</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"\n",
" <p><a href=\"https://cocl.us/DA0101EN_edx_link_Notebook_bottom\"><img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/Images/BottomAd.png\" width=\"750\" align=\"center\"></a></p>\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>About the Authors:</h3>\n",
"\n",
"This notebook was written by <a href=\"https://www.linkedin.com/in/mahdi-noorian-58219234/\" target=\"_blank\">Mahdi Noorian PhD</a>, <a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a>, Bahare Talayian, Eric Xiao, Steven Dong, Parizad, Hima Vsudevan and <a href=\"https://www.linkedin.com/in/fiorellawever/\" target=\"_blank\">Fiorella Wenver</a> and <a href=\" https://www.linkedin.com/in/yi-leng-yao-84451275/ \" target=\"_blank\" >Yi Yao</a>.\n",
"\n",
"<p><a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a> is a Data Scientist at IBM, and holds a PhD in Electrical Engineering. His research focused on using Machine Learning, Signal Processing, and Computer Vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<p>Copyright &copy; 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the <a href=\"https://cognitiveclass.ai/mit-license/\">MIT License</a>.</p>"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment