Skip to content

Instantly share code, notes, and snippets.

@kshirsagarsiddharth
Created December 21, 2019 09:09
Show Gist options
  • Select an option

  • Save kshirsagarsiddharth/b0e392136c512b76f37561ec9f01ad3b to your computer and use it in GitHub Desktop.

Select an option

Save kshirsagarsiddharth/b0e392136c512b76f37561ec9f01ad3b 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": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.datasets import load_iris\n",
"data = load_iris()\n",
"features = pd.DataFrame(data = data['data'],columns = data['feature_names'])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 35.0\n",
"1 30.0\n",
"2 32.0\n",
"3 31.0\n",
"4 36.0\n",
" ... \n",
"145 30.0\n",
"146 25.0\n",
"147 30.0\n",
"148 34.0\n",
"149 30.0\n",
"Name: sepal width (cm), Length: 150, dtype: float64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# we would like to change the measurement of thr sepal length from cm to mm\n",
"def cm_to_mm(cm):\n",
" mm = cm * 10\n",
" return mm\n",
"\n",
"features['sepal length (cm)'].map(cm_to_mm)\n",
"features['sepal width (cm)'].map(cm_to_mm)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'DataFrame' object has no attribute 'map'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-7293138f89d7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfeatures\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'sepal length (cm)'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'sepal width (cm)'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcm_to_mm\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/conda/envs/python/lib/python3.6/site-packages/pandas/core/generic.py\u001b[0m in \u001b[0;36m__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 5177\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_info_axis\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_can_hold_identifiers_and_holds_name\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5178\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 5179\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mobject\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__getattribute__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5180\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5181\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__setattr__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'DataFrame' object has no attribute 'map'"
]
}
],
"source": [
"features[['sepal length (cm)','sepal width (cm)']].map(cm_to_mm)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"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>sepal length (cm)</th>\n",
" <th>sepal width (cm)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>51.0</td>\n",
" <td>35.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>49.0</td>\n",
" <td>30.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47.0</td>\n",
" <td>32.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>46.0</td>\n",
" <td>31.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>50.0</td>\n",
" <td>36.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>145</th>\n",
" <td>67.0</td>\n",
" <td>30.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>146</th>\n",
" <td>63.0</td>\n",
" <td>25.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>65.0</td>\n",
" <td>30.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>62.0</td>\n",
" <td>34.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>59.0</td>\n",
" <td>30.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>150 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" sepal length (cm) sepal width (cm)\n",
"0 51.0 35.0\n",
"1 49.0 30.0\n",
"2 47.0 32.0\n",
"3 46.0 31.0\n",
"4 50.0 36.0\n",
".. ... ...\n",
"145 67.0 30.0\n",
"146 63.0 25.0\n",
"147 65.0 30.0\n",
"148 62.0 34.0\n",
"149 59.0 30.0\n",
"\n",
"[150 rows x 2 columns]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"features[['sepal length (cm)','sepal width (cm)']].apply(cm_to_mm)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<bound method DataFrame.info of sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n",
"0 5.1 3.5 1.4 0.2\n",
"1 4.9 3.0 1.4 0.2\n",
"2 4.7 3.2 1.3 0.2\n",
"3 4.6 3.1 1.5 0.2\n",
"4 5.0 3.6 1.4 0.2\n",
".. ... ... ... ...\n",
"145 6.7 3.0 5.2 2.3\n",
"146 6.3 2.5 5.0 1.9\n",
"147 6.5 3.0 5.2 2.0\n",
"148 6.2 3.4 5.4 2.3\n",
"149 5.9 3.0 5.1 1.8\n",
"\n",
"[150 rows x 4 columns]>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"features.info"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"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>sepal length (cm)</th>\n",
" <th>sepal width (cm)</th>\n",
" <th>petal length (cm)</th>\n",
" <th>petal width (cm)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>51.0</td>\n",
" <td>35.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>49.0</td>\n",
" <td>30.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47.0</td>\n",
" <td>32.0</td>\n",
" <td>13.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>46.0</td>\n",
" <td>31.0</td>\n",
" <td>15.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>50.0</td>\n",
" <td>36.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>145</th>\n",
" <td>67.0</td>\n",
" <td>30.0</td>\n",
" <td>52.0</td>\n",
" <td>23.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>146</th>\n",
" <td>63.0</td>\n",
" <td>25.0</td>\n",
" <td>50.0</td>\n",
" <td>19.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>65.0</td>\n",
" <td>30.0</td>\n",
" <td>52.0</td>\n",
" <td>20.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>62.0</td>\n",
" <td>34.0</td>\n",
" <td>54.0</td>\n",
" <td>23.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>59.0</td>\n",
" <td>30.0</td>\n",
" <td>51.0</td>\n",
" <td>18.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>150 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n",
"0 51.0 35.0 14.0 2.0\n",
"1 49.0 30.0 14.0 2.0\n",
"2 47.0 32.0 13.0 2.0\n",
"3 46.0 31.0 15.0 2.0\n",
"4 50.0 36.0 14.0 2.0\n",
".. ... ... ... ...\n",
"145 67.0 30.0 52.0 23.0\n",
"146 63.0 25.0 50.0 19.0\n",
"147 65.0 30.0 52.0 20.0\n",
"148 62.0 34.0 54.0 23.0\n",
"149 59.0 30.0 51.0 18.0\n",
"\n",
"[150 rows x 4 columns]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"features[['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']].apply(cm_to_mm)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"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>sepal length (cm)</th>\n",
" <th>sepal width (cm)</th>\n",
" <th>petal length (cm)</th>\n",
" <th>petal width (cm)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>51.0</td>\n",
" <td>35.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>49.0</td>\n",
" <td>30.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47.0</td>\n",
" <td>32.0</td>\n",
" <td>13.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>46.0</td>\n",
" <td>31.0</td>\n",
" <td>15.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>50.0</td>\n",
" <td>36.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n",
"0 51.0 35.0 14.0 2.0\n",
"1 49.0 30.0 14.0 2.0\n",
"2 47.0 32.0 13.0 2.0\n",
"3 46.0 31.0 15.0 2.0\n",
"4 50.0 36.0 14.0 2.0"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"features.applymap(cm_to_mm).head()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"iris = features.applymap(cm_to_mm).head()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"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>sepal length (cm)</th>\n",
" <th>sepal width (cm)</th>\n",
" <th>petal length (cm)</th>\n",
" <th>petal width (cm)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>51.0</td>\n",
" <td>35.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>49.0</td>\n",
" <td>30.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47.0</td>\n",
" <td>32.0</td>\n",
" <td>13.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>46.0</td>\n",
" <td>31.0</td>\n",
" <td>15.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>50.0</td>\n",
" <td>36.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n",
"0 51.0 35.0 14.0 2.0\n",
"1 49.0 30.0 14.0 2.0\n",
"2 47.0 32.0 13.0 2.0\n",
"3 46.0 31.0 15.0 2.0\n",
"4 50.0 36.0 14.0 2.0"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"iris.columns = ['sepal length (mm)','sepal width (mm)','petal length (mm)','petal width (mm)']"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"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>sepal length (mm)</th>\n",
" <th>sepal width (mm)</th>\n",
" <th>petal length (mm)</th>\n",
" <th>petal width (mm)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>51.0</td>\n",
" <td>35.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>49.0</td>\n",
" <td>30.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47.0</td>\n",
" <td>32.0</td>\n",
" <td>13.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>46.0</td>\n",
" <td>31.0</td>\n",
" <td>15.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>50.0</td>\n",
" <td>36.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal length (mm) sepal width (mm) petal length (mm) petal width (mm)\n",
"0 51.0 35.0 14.0 2.0\n",
"1 49.0 30.0 14.0 2.0\n",
"2 47.0 32.0 13.0 2.0\n",
"3 46.0 31.0 15.0 2.0\n",
"4 50.0 36.0 14.0 2.0"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris.head()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"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>sepal length (cm)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>51.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>49.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>46.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>50.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal length (cm)\n",
"0 51.0\n",
"1 49.0\n",
"2 47.0\n",
"3 46.0\n",
"4 50.0"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"features[['sepal length (cm)']].apply(lambda x: x*10).head()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"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>sepal length (cm)</th>\n",
" <th>sepal width (cm)</th>\n",
" <th>petal length (cm)</th>\n",
" <th>petal width (cm)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>51.0</td>\n",
" <td>35.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>49.0</td>\n",
" <td>30.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47.0</td>\n",
" <td>32.0</td>\n",
" <td>13.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>46.0</td>\n",
" <td>31.0</td>\n",
" <td>15.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>50.0</td>\n",
" <td>36.0</td>\n",
" <td>14.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n",
"0 51.0 35.0 14.0 2.0\n",
"1 49.0 30.0 14.0 2.0\n",
"2 47.0 32.0 13.0 2.0\n",
"3 46.0 31.0 15.0 2.0\n",
"4 50.0 36.0 14.0 2.0"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"features.applymap(lambda x : x * 10).head()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"col_name = ['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']\n",
"features['interaction'] = features.apply(lambda x : x[col_name[0]]*x[col_name[1]]*x[col_name[2]]*x[col_name[3]],axis = 1)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"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>sepal length (cm)</th>\n",
" <th>sepal width (cm)</th>\n",
" <th>petal length (cm)</th>\n",
" <th>petal width (cm)</th>\n",
" <th>interaction</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>3.5</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>4.9980</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>3.0</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>4.1160</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>3.2</td>\n",
" <td>1.3</td>\n",
" <td>0.2</td>\n",
" <td>3.9104</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>4.2780</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>3.6</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>5.0400</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>145</th>\n",
" <td>6.7</td>\n",
" <td>3.0</td>\n",
" <td>5.2</td>\n",
" <td>2.3</td>\n",
" <td>240.3960</td>\n",
" </tr>\n",
" <tr>\n",
" <th>146</th>\n",
" <td>6.3</td>\n",
" <td>2.5</td>\n",
" <td>5.0</td>\n",
" <td>1.9</td>\n",
" <td>149.6250</td>\n",
" </tr>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>6.5</td>\n",
" <td>3.0</td>\n",
" <td>5.2</td>\n",
" <td>2.0</td>\n",
" <td>202.8000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>6.2</td>\n",
" <td>3.4</td>\n",
" <td>5.4</td>\n",
" <td>2.3</td>\n",
" <td>261.8136</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>5.9</td>\n",
" <td>3.0</td>\n",
" <td>5.1</td>\n",
" <td>1.8</td>\n",
" <td>162.4860</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>150 rows × 5 columns</p>\n",
"</div>"
],
"text/plain": [
" sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \\\n",
"0 5.1 3.5 1.4 0.2 \n",
"1 4.9 3.0 1.4 0.2 \n",
"2 4.7 3.2 1.3 0.2 \n",
"3 4.6 3.1 1.5 0.2 \n",
"4 5.0 3.6 1.4 0.2 \n",
".. ... ... ... ... \n",
"145 6.7 3.0 5.2 2.3 \n",
"146 6.3 2.5 5.0 1.9 \n",
"147 6.5 3.0 5.2 2.0 \n",
"148 6.2 3.4 5.4 2.3 \n",
"149 5.9 3.0 5.1 1.8 \n",
"\n",
" interaction \n",
"0 4.9980 \n",
"1 4.1160 \n",
"2 3.9104 \n",
"3 4.2780 \n",
"4 5.0400 \n",
".. ... \n",
"145 240.3960 \n",
"146 149.6250 \n",
"147 202.8000 \n",
"148 261.8136 \n",
"149 162.4860 \n",
"\n",
"[150 rows x 5 columns]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"features"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"col_name = ['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']\n",
"features['interaction2'] = features.apply(lambda x : x[col_name[0]] * x[col_name[2]],axis = 1)\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"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>sepal length (cm)</th>\n",
" <th>sepal width (cm)</th>\n",
" <th>petal length (cm)</th>\n",
" <th>petal width (cm)</th>\n",
" <th>interaction</th>\n",
" <th>interaction2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>3.5</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>4.9980</td>\n",
" <td>7.14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>3.0</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>4.1160</td>\n",
" <td>6.86</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>3.2</td>\n",
" <td>1.3</td>\n",
" <td>0.2</td>\n",
" <td>3.9104</td>\n",
" <td>6.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>4.2780</td>\n",
" <td>6.90</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>3.6</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>5.0400</td>\n",
" <td>7.00</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \\\n",
"0 5.1 3.5 1.4 0.2 \n",
"1 4.9 3.0 1.4 0.2 \n",
"2 4.7 3.2 1.3 0.2 \n",
"3 4.6 3.1 1.5 0.2 \n",
"4 5.0 3.6 1.4 0.2 \n",
"\n",
" interaction interaction2 \n",
"0 4.9980 7.14 \n",
"1 4.1160 6.86 \n",
"2 3.9104 6.11 \n",
"3 4.2780 6.90 \n",
"4 5.0400 7.00 "
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"features.head()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Braund, Mr. Owen Harris</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Heikkinen, Miss. Laina</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Allen, Mr. William Henry</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Moran, Mr. James</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>McCarthy, Mr. Timothy J</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Palsson, Master. Gosta Leonard</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Nasser, Mrs. Nicholas (Adele Achem)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name\n",
"0 Braund, Mr. Owen Harris\n",
"1 Cumings, Mrs. John Bradley (Florence Briggs Th...\n",
"2 Heikkinen, Miss. Laina\n",
"3 Futrelle, Mrs. Jacques Heath (Lily May Peel)\n",
"4 Allen, Mr. William Henry\n",
"5 Moran, Mr. James\n",
"6 McCarthy, Mr. Timothy J\n",
"7 Palsson, Master. Gosta Leonard\n",
"8 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)\n",
"9 Nasser, Mrs. Nicholas (Adele Achem)"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"name = pd.DataFrame(data = ['Braund, Mr. Owen Harris',\n",
" 'Cumings, Mrs. John Bradley (Florence Briggs Thayer)',\n",
" 'Heikkinen, Miss. Laina',\n",
" 'Futrelle, Mrs. Jacques Heath (Lily May Peel)',\n",
" 'Allen, Mr. William Henry',\n",
" 'Moran, Mr. James',\n",
" 'McCarthy, Mr. Timothy J',\n",
" 'Palsson, Master. Gosta Leonard',\n",
" 'Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)',\n",
" 'Nasser, Mrs. Nicholas (Adele Achem)'], columns = ['Name'] )\n",
"\n",
"#Take a look at the Data \n",
"name"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Title</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Braund, Mr. Owen Harris</td>\n",
" <td>Mr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
" <td>Mrs</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Heikkinen, Miss. Laina</td>\n",
" <td>Miss</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
" <td>Mrs</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Allen, Mr. William Henry</td>\n",
" <td>Mr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Moran, Mr. James</td>\n",
" <td>Mr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>McCarthy, Mr. Timothy J</td>\n",
" <td>Mr</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Palsson, Master. Gosta Leonard</td>\n",
" <td>Master</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)</td>\n",
" <td>Mrs</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Nasser, Mrs. Nicholas (Adele Achem)</td>\n",
" <td>Mrs</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name Title\n",
"0 Braund, Mr. Owen Harris Mr \n",
"1 Cumings, Mrs. John Bradley (Florence Briggs Th... Mrs \n",
"2 Heikkinen, Miss. Laina Miss \n",
"3 Futrelle, Mrs. Jacques Heath (Lily May Peel) Mrs \n",
"4 Allen, Mr. William Henry Mr \n",
"5 Moran, Mr. James Mr \n",
"6 McCarthy, Mr. Timothy J Mr \n",
"7 Palsson, Master. Gosta Leonard Master \n",
"8 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) Mrs \n",
"9 Nasser, Mrs. Nicholas (Adele Achem) Mrs "
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#lets say we want to extract the Titles of each name we can do this:\n",
"name['Name'].apply(lambda x : x.split(\" \")[1].replace(\".\",\" \"))\n",
"name['Title'] = name['Name'].apply(lambda x : x.split(\" \")[1].replace(\".\",\" \"))\n",
"name\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"name['Name'].apply(lambda x : x.split(\" \")[0].replace(\",\",\" \"))\n",
"name['LastName'] = name['Name'].apply(lambda x : x.split(\" \")[0].replace(\",\",\" \"))"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Title</th>\n",
" <th>LastName</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Braund, Mr. Owen Harris</td>\n",
" <td>Mr</td>\n",
" <td>Braund</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
" <td>Mrs</td>\n",
" <td>Cumings</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Heikkinen, Miss. Laina</td>\n",
" <td>Miss</td>\n",
" <td>Heikkinen</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
" <td>Mrs</td>\n",
" <td>Futrelle</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Allen, Mr. William Henry</td>\n",
" <td>Mr</td>\n",
" <td>Allen</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Moran, Mr. James</td>\n",
" <td>Mr</td>\n",
" <td>Moran</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>McCarthy, Mr. Timothy J</td>\n",
" <td>Mr</td>\n",
" <td>McCarthy</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Palsson, Master. Gosta Leonard</td>\n",
" <td>Master</td>\n",
" <td>Palsson</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)</td>\n",
" <td>Mrs</td>\n",
" <td>Johnson</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Nasser, Mrs. Nicholas (Adele Achem)</td>\n",
" <td>Mrs</td>\n",
" <td>Nasser</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name Title LastName\n",
"0 Braund, Mr. Owen Harris Mr Braund \n",
"1 Cumings, Mrs. John Bradley (Florence Briggs Th... Mrs Cumings \n",
"2 Heikkinen, Miss. Laina Miss Heikkinen \n",
"3 Futrelle, Mrs. Jacques Heath (Lily May Peel) Mrs Futrelle \n",
"4 Allen, Mr. William Henry Mr Allen \n",
"5 Moran, Mr. James Mr Moran \n",
"6 McCarthy, Mr. Timothy J Mr McCarthy \n",
"7 Palsson, Master. Gosta Leonard Master Palsson \n",
"8 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) Mrs Johnson \n",
"9 Nasser, Mrs. Nicholas (Adele Achem) Mrs Nasser "
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"name\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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