Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Created on Cognitive Class Labs
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"https://cocl.us/corsera_da0101en_notebook_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>"
]
},
{
"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": 24,
"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/corsera_da0101en_notebook_bottom\">HERE</a> for free storage "
]
},
{
"cell_type": "code",
"execution_count": 25,
"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": 26,
"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": 28,
"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": 29,
"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": 29,
"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": 30,
"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": 30,
"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": 31,
"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": 31,
"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": 32,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"symboling\n",
"False 205\n",
"Name: symboling, dtype: int64\n",
"\n",
"normalized-losses\n",
"False 164\n",
"True 41\n",
"Name: normalized-losses, dtype: int64\n",
"\n",
"make\n",
"False 205\n",
"Name: make, dtype: int64\n",
"\n",
"fuel-type\n",
"False 205\n",
"Name: fuel-type, dtype: int64\n",
"\n",
"aspiration\n",
"False 205\n",
"Name: aspiration, dtype: int64\n",
"\n",
"num-of-doors\n",
"False 203\n",
"True 2\n",
"Name: num-of-doors, dtype: int64\n",
"\n",
"body-style\n",
"False 205\n",
"Name: body-style, dtype: int64\n",
"\n",
"drive-wheels\n",
"False 205\n",
"Name: drive-wheels, dtype: int64\n",
"\n",
"engine-location\n",
"False 205\n",
"Name: engine-location, dtype: int64\n",
"\n",
"wheel-base\n",
"False 205\n",
"Name: wheel-base, dtype: int64\n",
"\n",
"length\n",
"False 205\n",
"Name: length, dtype: int64\n",
"\n",
"width\n",
"False 205\n",
"Name: width, dtype: int64\n",
"\n",
"height\n",
"False 205\n",
"Name: height, dtype: int64\n",
"\n",
"curb-weight\n",
"False 205\n",
"Name: curb-weight, dtype: int64\n",
"\n",
"engine-type\n",
"False 205\n",
"Name: engine-type, dtype: int64\n",
"\n",
"num-of-cylinders\n",
"False 205\n",
"Name: num-of-cylinders, dtype: int64\n",
"\n",
"engine-size\n",
"False 205\n",
"Name: engine-size, dtype: int64\n",
"\n",
"fuel-system\n",
"False 205\n",
"Name: fuel-system, 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",
"compression-ratio\n",
"False 205\n",
"Name: compression-ratio, 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",
"city-mpg\n",
"False 205\n",
"Name: city-mpg, dtype: int64\n",
"\n",
"highway-mpg\n",
"False 205\n",
"Name: highway-mpg, 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",
" 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": 33,
"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": 34,
"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": 35,
"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": 36,
"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": 37,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"average of stroke is: 3.255422885572139\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"stroke_avg = df['stroke'].astype('float').mean(axis=0)\n",
"print(\"average of stroke is:\", stroke_avg)\n",
"df['stroke'].replace(np.nan, stroke_avg, inplace=True)\n",
"\n"
]
},
{
"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": 38,
"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": 45,
"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": 46,
"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": 47,
"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": 48,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"four 116\n",
"two 89\n",
"Name: num-of-doors, dtype: int64"
]
},
"execution_count": 48,
"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": 49,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'four'"
]
},
"execution_count": 49,
"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": 50,
"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": 51,
"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": 52,
"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": 52,
"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": 53,
"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": 53,
"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": 54,
"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": 55,
"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": 55,
"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": 56,
"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": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 71,
"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-L/100km</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>8.703704</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>8.703704</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>9.038462</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>7.833333</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>10.681818</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-L/100km price city-L/100km \n",
"0 8.703704 13495.0 11.190476 \n",
"1 8.703704 16500.0 11.190476 \n",
"2 9.038462 16500.0 12.368421 \n",
"3 7.833333 13950.0 9.791667 \n",
"4 10.681818 17450.0 13.055556 \n",
"\n",
"[5 rows x 27 columns]"
]
},
"execution_count": 71,
"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()\n"
]
},
{
"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": 78,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 8.703704\n",
"1 8.703704\n",
"2 9.038462\n",
"3 7.833333\n",
"4 10.681818\n",
" ... \n",
"196 8.392857\n",
"197 9.400000\n",
"198 10.217391\n",
"199 8.703704\n",
"200 9.400000\n",
"Name: highway-L/100km, Length: 201, dtype: float64"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"df['highway-L/100km']"
]
},
{
"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": 79,
"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": 83,
"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": 83,
"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": 84,
"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": 85,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(0.5, 1.0, 'horsepower bins')"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEWCAYAAABhffzLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAVC0lEQVR4nO3de7SldX3f8feHAUXlHgYcwThIiStoDeoEtRSkEBXBZqiJF1awU0OCdqnRVU0yJk1q2piCMSbGusrCahjjldQg09il0lEGSRQdDCKIBoKDF0ZmuChoWxT89o/nN2Uzc87hAPOcfeb83q+19trPdT/f/Zs9n/07z372b6eqkCT1Y49pFyBJWlgGvyR1xuCXpM4Y/JLUGYNfkjpj8EtSZwx+TVWSzUl+Ydp1LHZJTkzy7TnWn5fk9xayJu2+9px2AZIevqp61bRr0O7DHr+WhCRLphOzlJ6LFieDX4vB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CH+Y+DSqtrWln8AOKGqPpZknzaO1OOBDzL8CMzx7VhPAp4CXDIMA8MyhqEkZqpTesgMfi1Fd09M3wscMNuGzQ+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",