{ "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%2F02_select_columns.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/02_select_columns.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #2 ([video](https://www.youtube.com/watch?v=sCt4LVD5hPc&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=2))\n", "\n", "There are SEVEN ways to select columns using ColumnTransformer:\n", "\n", "1. column name\n", "2. integer position\n", "3. slice\n", "4. boolean mask\n", "5. regex pattern\n", "6. dtypes to include\n", "7. dtypes to exclude\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", "