{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "pandas-select-columns.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "sBn4_vrWHjCi", "colab_type": "text" }, "source": [ "# **Introduction to Pandas dataframe: Select specific column(s) in a DataFrame**\n", "\n", "Chanin Nantasenamat\n", "\n", "[*'Data Professor' YouTube channel*](http://youtube.com/dataprofessor)\n", "\n", "In this Jupyter notebook, I will be showing you how to select specifc column(s) in a DataFrame.\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": { "id": "PBWDV57tHmLf", "colab_type": "text" }, "source": [ "## **Download CSV data**" ] }, { "cell_type": "code", "metadata": { "id": "pq6EVr5sHXAq", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 208 }, "outputId": "1eea0330-050d-4101-c21b-dce27e09cf01" }, "source": [ "! wget https://raw.githubusercontent.com/dataprofessor/data/master/delaney_solubility_with_descriptors.csv" ], "execution_count": 10, "outputs": [ { "output_type": "stream", "text": [ "--2020-05-03 17:41:00-- https://raw.githubusercontent.com/dataprofessor/data/master/delaney_solubility_with_descriptors.csv\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 57370 (56K) [text/plain]\n", "Saving to: ‘delaney_solubility_with_descriptors.csv’\n", "\n", "\r delaney_s 0%[ ] 0 --.-KB/s \rdelaney_solubility_ 100%[===================>] 56.03K --.-KB/s in 0.01s \n", "\n", "2020-05-03 17:41:00 (3.82 MB/s) - ‘delaney_solubility_with_descriptors.csv’ saved [57370/57370]\n", "\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "qJ9j-V_zHwm8", "colab_type": "code", "colab": {} }, "source": [ "import pandas as pd" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "D6twMV5THz2r", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 415 }, "outputId": "3230bda2-d61e-48f6-f232-0dabecc08908" }, "source": [ "df = pd.read_csv('delaney_solubility_with_descriptors.csv')\n", "df" ], "execution_count": 12, "outputs": [ { "output_type": "execute_result", "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", " \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", " \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", "
MolLogPMolWtNumRotatableBondsAromaticProportionlogS
02.59540167.8500.00.000000-2.180
12.37650133.4050.00.000000-2.000
22.59380167.8501.00.000000-1.740
32.02890133.4051.00.000000-1.480
42.91890187.3751.00.000000-3.040
..................
11391.98820287.3438.00.0000001.144
11403.42130286.1142.00.333333-4.925
11413.60960308.3334.00.695652-3.893
11422.56214354.8153.00.521739-3.790
11432.02164179.2191.00.461538-2.581
\n", "

1144 rows × 5 columns

\n", "
" ], "text/plain": [ " MolLogP MolWt NumRotatableBonds AromaticProportion logS\n", "0 2.59540 167.850 0.0 0.000000 -2.180\n", "1 2.37650 133.405 0.0 0.000000 -2.000\n", "2 2.59380 167.850 1.0 0.000000 -1.740\n", "3 2.02890 133.405 1.0 0.000000 -1.480\n", "4 2.91890 187.375 1.0 0.000000 -3.040\n", "... ... ... ... ... ...\n", "1139 1.98820 287.343 8.0 0.000000 1.144\n", "1140 3.42130 286.114 2.0 0.333333 -4.925\n", "1141 3.60960 308.333 4.0 0.695652 -3.893\n", "1142 2.56214 354.815 3.0 0.521739 -3.790\n", "1143 2.02164 179.219 1.0 0.461538 -2.581\n", "\n", "[1144 rows x 5 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 12 } ] }, { "cell_type": "markdown", "metadata": { "id": "uc36WETtHp77", "colab_type": "text" }, "source": [ "## **Selecting specific column(s)**" ] }, { "cell_type": "markdown", "metadata": { "id": "Ha6n6gH3IsPs", "colab_type": "text" }, "source": [ "### Selecting a single column" ] }, { "cell_type": "code", "metadata": { "id": "94xKLaCoHpeY", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 225 }, "outputId": "7bc417a4-ff9e-43c4-f05a-7dc4eb5aa387" }, "source": [ "df.MolLogP" ], "execution_count": 13, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0 2.59540\n", "1 2.37650\n", "2 2.59380\n", "3 2.02890\n", "4 2.91890\n", " ... \n", "1139 1.98820\n", "1140 3.42130\n", "1141 3.60960\n", "1142 2.56214\n", "1143 2.02164\n", "Name: MolLogP, Length: 1144, dtype: float64" ] }, "metadata": { "tags": [] }, "execution_count": 13 } ] }, { "cell_type": "code", "metadata": { "id": "ua7cZKWzIP-J", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 225 }, "outputId": "3cff5ff9-f26a-43ea-9c53-7772d68c48e2" }, "source": [ "df['MolLogP']" ], "execution_count": 14, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0 2.59540\n", "1 2.37650\n", "2 2.59380\n", "3 2.02890\n", "4 2.91890\n", " ... \n", "1139 1.98820\n", "1140 3.42130\n", "1141 3.60960\n", "1142 2.56214\n", "1143 2.02164\n", "Name: MolLogP, Length: 1144, dtype: float64" ] }, "metadata": { "tags": [] }, "execution_count": 14 } ] }, { "cell_type": "markdown", "metadata": { "id": "Mb_loyXfIyhw", "colab_type": "text" }, "source": [ "### Selecting two or more columns" ] }, { "cell_type": "code", "metadata": { "id": "ZbLCqhFbIRfS", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 415 }, "outputId": "d1d61288-290a-4fce-db4b-e3fbf635ba4d" }, "source": [ "df[['MolLogP','NumRotatableBonds']]" ], "execution_count": 23, "outputs": [ { "output_type": "execute_result", "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", " \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", "
MolLogPNumRotatableBonds
02.595400.0
12.376500.0
22.593801.0
32.028901.0
42.918901.0
.........
11391.988208.0
11403.421302.0
11413.609604.0
11422.562143.0
11432.021641.0
\n", "

1144 rows × 2 columns

\n", "
" ], "text/plain": [ " MolLogP NumRotatableBonds\n", "0 2.59540 0.0\n", "1 2.37650 0.0\n", "2 2.59380 1.0\n", "3 2.02890 1.0\n", "4 2.91890 1.0\n", "... ... ...\n", "1139 1.98820 8.0\n", "1140 3.42130 2.0\n", "1141 3.60960 4.0\n", "1142 2.56214 3.0\n", "1143 2.02164 1.0\n", "\n", "[1144 rows x 2 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 23 } ] }, { "cell_type": "code", "metadata": { "id": "qxMA09nEIV7e", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 415 }, "outputId": "6f1131df-d3ff-4735-96d5-166a11c03f56" }, "source": [ "df.iloc[:,[0,2]]" ], "execution_count": 22, "outputs": [ { "output_type": "execute_result", "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", " \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", "
MolLogPNumRotatableBonds
02.595400.0
12.376500.0
22.593801.0
32.028901.0
42.918901.0
.........
11391.988208.0
11403.421302.0
11413.609604.0
11422.562143.0
11432.021641.0
\n", "

1144 rows × 2 columns

\n", "
" ], "text/plain": [ " MolLogP NumRotatableBonds\n", "0 2.59540 0.0\n", "1 2.37650 0.0\n", "2 2.59380 1.0\n", "3 2.02890 1.0\n", "4 2.91890 1.0\n", "... ... ...\n", "1139 1.98820 8.0\n", "1140 3.42130 2.0\n", "1141 3.60960 4.0\n", "1142 2.56214 3.0\n", "1143 2.02164 1.0\n", "\n", "[1144 rows x 2 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 22 } ] }, { "cell_type": "code", "metadata": { "id": "V53KSsZjId1X", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 415 }, "outputId": "81be4e24-2bac-4dd5-97d7-32ece84768c6" }, "source": [ "selection = ['MolLogP','NumRotatableBonds', 'logS']\n", "df[selection]" ], "execution_count": 25, "outputs": [ { "output_type": "execute_result", "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", " \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", " \n", "
MolLogPNumRotatableBondslogS
02.595400.0-2.180
12.376500.0-2.000
22.593801.0-1.740
32.028901.0-1.480
42.918901.0-3.040
............
11391.988208.01.144
11403.421302.0-4.925
11413.609604.0-3.893
11422.562143.0-3.790
11432.021641.0-2.581
\n", "

1144 rows × 3 columns

\n", "
" ], "text/plain": [ " MolLogP NumRotatableBonds logS\n", "0 2.59540 0.0 -2.180\n", "1 2.37650 0.0 -2.000\n", "2 2.59380 1.0 -1.740\n", "3 2.02890 1.0 -1.480\n", "4 2.91890 1.0 -3.040\n", "... ... ... ...\n", "1139 1.98820 8.0 1.144\n", "1140 3.42130 2.0 -4.925\n", "1141 3.60960 4.0 -3.893\n", "1142 2.56214 3.0 -3.790\n", "1143 2.02164 1.0 -2.581\n", "\n", "[1144 rows x 3 columns]" ] }, "metadata": { "tags": [] }, "execution_count": 25 } ] }, { "cell_type": "code", "metadata": { "id": "rF85rPt1I9sY", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": 0, "outputs": [] } ] }