{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open in Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/justmarkham/scikit-learn-tips/master?filepath=notebooks%2F42_passthrough_or_drop.ipynb)\n", "\n", "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/justmarkham/scikit-learn-tips/blob/master/notebooks/42_passthrough_or_drop.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #42 ([video](https://www.youtube.com/watch?v=vHGRXuOtFnE&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=42))\n", "\n", "In a ColumnTransformer, you can use the strings 'passthrough' and 'drop' in place of a transformer. Useful if you need to passthrough some columns and drop others!\n", "\n", "See example 👇" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from sklearn.impute import SimpleImputer\n", "from sklearn.compose import make_column_transformer" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "impute = SimpleImputer()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "X = pd.DataFrame({'A':[1, 2, np.nan],\n", " 'B':[10, 20, 30],\n", " 'C':[100, 200, 300],\n", " 'D':[1000, 2000, 3000],\n", " 'E':[10000, 20000, 30000]})" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ABCDE
01.010100100010000
12.020200200020000
2NaN30300300030000
\n", "
" ], "text/plain": [ " A B C D E\n", "0 1.0 10 100 1000 10000\n", "1 2.0 20 200 2000 20000\n", "2 NaN 30 300 3000 30000" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# impute A, passthrough B & C, then drop the remaining columns\n", "ct = make_column_transformer(\n", " (impute, ['A']),\n", " ('passthrough', ['B', 'C']),\n", " remainder='drop')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1. , 10. , 100. ],\n", " [ 2. , 20. , 200. ],\n", " [ 1.5, 30. , 300. ]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ct.fit_transform(X)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# impute A, drop D & E, then passthrough the remaining columns\n", "ct = make_column_transformer(\n", " (impute, ['A']),\n", " ('drop', ['D', 'E']),\n", " remainder='passthrough')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1. , 10. , 100. ],\n", " [ 2. , 20. , 200. ],\n", " [ 1.5, 30. , 300. ]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ct.fit_transform(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Want more tips? [View all tips on GitHub](https://github.com/justmarkham/scikit-learn-tips) or [Sign up to receive 2 tips by email every week](https://scikit-learn.tips) 💌\n", "\n", "© 2020 [Data School](https://www.dataschool.io). All rights reserved." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }