{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[](https://mybinder.org/v2/gh/justmarkham/scikit-learn-tips/master?filepath=notebooks%2F01_column_transformer.ipynb)\n", "\n", "[](https://colab.research.google.com/github/justmarkham/scikit-learn-tips/blob/master/notebooks/01_column_transformer.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #1 ([video](https://www.youtube.com/watch?v=NGq8wnH5VSo&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=1))\n", "\n", "Use ColumnTransformer to apply different preprocessing to different columns:\n", "\n", "- select from DataFrame columns by name\n", "- passthrough or drop unspecified columns\n", "\n", "Requires scikit-learn 0.20+\n", "\n", "See example 👇" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "df = pd.read_csv('http://bit.ly/kaggletrain', nrows=6)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "cols = ['Fare', 'Embarked', 'Sex', 'Age']\n", "X = df[cols]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | Fare | \n", "Embarked | \n", "Sex | \n", "Age | \n", "
|---|---|---|---|---|
| 0 | \n", "7.2500 | \n", "S | \n", "male | \n", "22.0 | \n", "
| 1 | \n", "71.2833 | \n", "C | \n", "female | \n", "38.0 | \n", "
| 2 | \n", "7.9250 | \n", "S | \n", "female | \n", "26.0 | \n", "
| 3 | \n", "53.1000 | \n", "S | \n", "female | \n", "35.0 | \n", "
| 4 | \n", "8.0500 | \n", "S | \n", "male | \n", "35.0 | \n", "
| 5 | \n", "8.4583 | \n", "Q | \n", "male | \n", "NaN | \n", "