{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "In this blog post, I will show the different ways to select subsets of data in Pandas using [], .loc, .iloc, .at, and .iat.  I will be using the wine quality dataset hosted on the UCI website. This data record 11 chemical properties (such as the concentrations of sugar, citric acid, alcohol, pH etc.) of thousands of red and white wines from northern Portugal, as well as the quality of the wines, recorded on a scale from 1 to 10. We will only look at the data for red wine." ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "import pandas as pd \n", "wine_df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv', sep=';')" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidepH_5sulphatesalcoholquality_6
density
0.99787.40.700.001.90.07611.034.03.510.569.45
0.99687.80.880.002.60.09825.067.03.200.689.85
0.99707.80.760.042.30.09215.054.03.260.659.85
0.998011.20.280.561.90.07517.060.03.160.589.86
0.99647.90.600.061.60.06915.059.03.300.469.45
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar \\\n", "density \n", "0.9978 7.4 0.70 0.00 1.9 \n", "0.9968 7.8 0.88 0.00 2.6 \n", "0.9970 7.8 0.76 0.04 2.3 \n", "0.9980 11.2 0.28 0.56 1.9 \n", "0.9964 7.9 0.60 0.06 1.6 \n", "\n", " chlorides free_sulfur_dioxide total_sulfur_dioxide pH_5 \\\n", "density \n", "0.9978 0.076 11.0 34.0 3.51 \n", "0.9968 0.098 25.0 67.0 3.20 \n", "0.9970 0.092 15.0 54.0 3.26 \n", "0.9980 0.075 17.0 60.0 3.16 \n", "0.9964 0.069 15.0 59.0 3.30 \n", "\n", " sulphates alcohol quality_6 \n", "density \n", "0.9978 0.56 9.4 5 \n", "0.9968 0.68 9.8 5 \n", "0.9970 0.65 9.8 5 \n", "0.9980 0.58 9.8 6 \n", "0.9964 0.46 9.4 5 " ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Different ways to select columns" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "wine_df.columns = ['fixed_acidity', 'volatile_acidity', 'citric_acid', 'residual_sugar', 'chlorides', 'free_sulfur_dioxide', 'total_sulfur_dioxide','density','pH','sulphates', 'alcohol', 'quality' ]" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 7.4\n", "1 7.8\n", "2 7.8\n", "3 11.2\n", "4 7.4\n", "Name: fixed_acidity, dtype: float64" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df['fixed_acidity'].head()" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 7.4\n", "1 7.8\n", "2 7.8\n", "3 11.2\n", "4 7.4\n", "Name: fixed_acidity, dtype: float64" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.fixed_acidity.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selecting multiple columns" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [], "source": [ "wine_four = wine_df[['fixed_acidity', 'volatile_acidity','citric_acid', 'residual_sugar']]" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [], "source": [ "cols = ['fixed_acidity', 'volatile_acidity','citric_acid', 'residual_sugar']\n", "wine_list_four = wine_four[cols]" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugar
07.40.700.001.9
17.80.880.002.6
27.80.760.042.3
311.20.280.561.9
47.40.700.001.9
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar\n", "0 7.4 0.70 0.00 1.9\n", "1 7.8 0.88 0.00 2.6\n", "2 7.8 0.76 0.04 2.3\n", "3 11.2 0.28 0.56 1.9\n", "4 7.4 0.70 0.00 1.9" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_list_four.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selecting columns using \"select_dtypes\" and \"filter\" methods" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float64 11\n", "int64 1\n", "dtype: int64" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.get_dtype_counts()" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidedensitypHsulphatesalcohol
07.40.700.001.90.07611.034.00.99783.510.569.4
17.80.880.002.60.09825.067.00.99683.200.689.8
27.80.760.042.30.09215.054.00.99703.260.659.8
311.20.280.561.90.07517.060.00.99803.160.589.8
47.40.700.001.90.07611.034.00.99783.510.569.4
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar chlorides \\\n", "0 7.4 0.70 0.00 1.9 0.076 \n", "1 7.8 0.88 0.00 2.6 0.098 \n", "2 7.8 0.76 0.04 2.3 0.092 \n", "3 11.2 0.28 0.56 1.9 0.075 \n", "4 7.4 0.70 0.00 1.9 0.076 \n", "\n", " free_sulfur_dioxide total_sulfur_dioxide density pH sulphates \\\n", "0 11.0 34.0 0.9978 3.51 0.56 \n", "1 25.0 67.0 0.9968 3.20 0.68 \n", "2 15.0 54.0 0.9970 3.26 0.65 \n", "3 17.0 60.0 0.9980 3.16 0.58 \n", "4 11.0 34.0 0.9978 3.51 0.56 \n", "\n", " alcohol \n", "0 9.4 \n", "1 9.8 \n", "2 9.8 \n", "3 9.8 \n", "4 9.4 " ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.select_dtypes(include = ['float']).head()" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidedensitypHsulphatesalcoholquality
07.40.700.001.90.07611.034.00.99783.510.569.45
17.80.880.002.60.09825.067.00.99683.200.689.85
27.80.760.042.30.09215.054.00.99703.260.659.85
311.20.280.561.90.07517.060.00.99803.160.589.86
47.40.700.001.90.07611.034.00.99783.510.569.45
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar chlorides \\\n", "0 7.4 0.70 0.00 1.9 0.076 \n", "1 7.8 0.88 0.00 2.6 0.098 \n", "2 7.8 0.76 0.04 2.3 0.092 \n", "3 11.2 0.28 0.56 1.9 0.075 \n", "4 7.4 0.70 0.00 1.9 0.076 \n", "\n", " free_sulfur_dioxide total_sulfur_dioxide density pH sulphates \\\n", "0 11.0 34.0 0.9978 3.51 0.56 \n", "1 25.0 67.0 0.9968 3.20 0.68 \n", "2 15.0 54.0 0.9970 3.26 0.65 \n", "3 17.0 60.0 0.9980 3.16 0.58 \n", "4 11.0 34.0 0.9978 3.51 0.56 \n", "\n", " alcohol quality \n", "0 9.4 5 \n", "1 9.8 5 \n", "2 9.8 5 \n", "3 9.8 6 \n", "4 9.4 5 " ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.select_dtypes(include=['number']).head()" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "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", " \n", " \n", " \n", " \n", "
fixed_acidityvolatile_aciditycitric_acid
07.40.700.00
17.80.880.00
27.80.760.04
311.20.280.56
47.40.700.00
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid\n", "0 7.4 0.70 0.00\n", "1 7.8 0.88 0.00\n", "2 7.8 0.76 0.04\n", "3 11.2 0.28 0.56\n", "4 7.4 0.70 0.00" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.filter(like='acid').head()" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "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", " \n", " \n", " \n", "
pH_5quality_6
density
0.99783.515
0.99683.205
0.99703.265
0.99803.166
0.99643.305
\n", "
" ], "text/plain": [ " pH_5 quality_6\n", "density \n", "0.9978 3.51 5\n", "0.9968 3.20 5\n", "0.9970 3.26 5\n", "0.9980 3.16 6\n", "0.9964 3.30 5" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.rename(columns={'pH':'pH_5', 'quality': 'quality_6' }, inplace=True)\n", "wine_df.filter(regex='\\d').head()" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidedensitypH_5sulphatesalcoholquality_6
07.40.700.001.90.07611.034.00.99783.510.569.45
17.80.880.002.60.09825.067.00.99683.200.689.85
27.80.760.042.30.09215.054.00.99703.260.659.85
311.20.280.561.90.07517.060.00.99803.160.589.86
47.40.700.001.90.07611.034.00.99783.510.569.45
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar chlorides \\\n", "0 7.4 0.70 0.00 1.9 0.076 \n", "1 7.8 0.88 0.00 2.6 0.098 \n", "2 7.8 0.76 0.04 2.3 0.092 \n", "3 11.2 0.28 0.56 1.9 0.075 \n", "4 7.4 0.70 0.00 1.9 0.076 \n", "\n", " free_sulfur_dioxide total_sulfur_dioxide density pH_5 sulphates \\\n", "0 11.0 34.0 0.9978 3.51 0.56 \n", "1 25.0 67.0 0.9968 3.20 0.68 \n", "2 15.0 54.0 0.9970 3.26 0.65 \n", "3 17.0 60.0 0.9980 3.16 0.58 \n", "4 11.0 34.0 0.9978 3.51 0.56 \n", "\n", " alcohol quality_6 \n", "0 9.4 5 \n", "1 9.8 5 \n", "2 9.8 5 \n", "3 9.8 6 \n", "4 9.4 5 " ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.head()" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "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", " \n", " \n", " \n", "
fixed_acidityvolatile_acidity
density
0.99787.40.70
0.99687.80.88
0.99707.80.76
0.998011.20.28
0.99647.90.60
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity\n", "density \n", "0.9978 7.4 0.70\n", "0.9968 7.8 0.88\n", "0.9970 7.8 0.76\n", "0.9980 11.2 0.28\n", "0.9964 7.9 0.60" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.filter(items=['fixed_acidity', 'volatile_acidity']).head()" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidedensitypH_5sulphatesalcoholquality_6
07.40.700.001.90.07611.034.00.99783.510.569.45
17.80.880.002.60.09825.067.00.99683.200.689.85
27.80.760.042.30.09215.054.00.99703.260.659.85
311.20.280.561.90.07517.060.00.99803.160.589.86
47.40.700.001.90.07611.034.00.99783.510.569.45
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar chlorides \\\n", "0 7.4 0.70 0.00 1.9 0.076 \n", "1 7.8 0.88 0.00 2.6 0.098 \n", "2 7.8 0.76 0.04 2.3 0.092 \n", "3 11.2 0.28 0.56 1.9 0.075 \n", "4 7.4 0.70 0.00 1.9 0.076 \n", "\n", " free_sulfur_dioxide total_sulfur_dioxide density pH_5 sulphates \\\n", "0 11.0 34.0 0.9978 3.51 0.56 \n", "1 25.0 67.0 0.9968 3.20 0.68 \n", "2 15.0 54.0 0.9970 3.26 0.65 \n", "3 17.0 60.0 0.9980 3.16 0.58 \n", "4 11.0 34.0 0.9978 3.51 0.56 \n", "\n", " alcohol quality_6 \n", "0 9.4 5 \n", "1 9.8 5 \n", "2 9.8 5 \n", "3 9.8 6 \n", "4 9.4 5 " ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing the order of your columns" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['fixed_acidity', 'volatile_acidity', 'citric_acid', 'residual_sugar',\n", " 'chlorides', 'free_sulfur_dioxide', 'total_sulfur_dioxide', 'density',\n", " 'pH_5', 'sulphates', 'alcohol', 'quality_6'],\n", " dtype='object')" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.columns" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "group_1 = ['pH_5','sulphates','alcohol', 'quality_6']\n", "group_2 =['chlorides', 'free_sulfur_dioxide', 'total_sulfur_dioxide', 'density']\n", "group_3 = ['fixed_acidity', 'volatile_acidity', 'citric_acid', 'residual_sugar']" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "new_cols = group_1+group_2+group_3" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['fixed_acidity', 'volatile_acidity', 'citric_acid', 'residual_sugar',\n", " 'chlorides', 'free_sulfur_dioxide', 'total_sulfur_dioxide', 'density',\n", " 'pH_5', 'sulphates', 'alcohol', 'quality_6'],\n", " dtype='object')" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.columns " ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(wine_df.columns) == set(new_cols) " ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [], "source": [ "wine_df_2 = wine_df[new_cols]" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "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", " \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", "
pH_5sulphatesalcoholquality_6chloridesfree_sulfur_dioxidetotal_sulfur_dioxidedensityfixed_acidityvolatile_aciditycitric_acidresidual_sugar
03.510.569.450.07611.034.00.99787.40.700.001.9
13.200.689.850.09825.067.00.99687.80.880.002.6
23.260.659.850.09215.054.00.99707.80.760.042.3
33.160.589.860.07517.060.00.998011.20.280.561.9
43.510.569.450.07611.034.00.99787.40.700.001.9
\n", "
" ], "text/plain": [ " pH_5 sulphates alcohol quality_6 chlorides free_sulfur_dioxide \\\n", "0 3.51 0.56 9.4 5 0.076 11.0 \n", "1 3.20 0.68 9.8 5 0.098 25.0 \n", "2 3.26 0.65 9.8 5 0.092 15.0 \n", "3 3.16 0.58 9.8 6 0.075 17.0 \n", "4 3.51 0.56 9.4 5 0.076 11.0 \n", "\n", " total_sulfur_dioxide density fixed_acidity volatile_acidity \\\n", "0 34.0 0.9978 7.4 0.70 \n", "1 67.0 0.9968 7.8 0.88 \n", "2 54.0 0.9970 7.8 0.76 \n", "3 60.0 0.9980 11.2 0.28 \n", "4 34.0 0.9978 7.4 0.70 \n", "\n", " citric_acid residual_sugar \n", "0 0.00 1.9 \n", "1 0.00 2.6 \n", "2 0.04 2.3 \n", "3 0.56 1.9 \n", "4 0.00 1.9 " ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df_2.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Selecting rows using .iloc and loc\n" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "fixed_acidity 96\n", "volatile_acidity 143\n", "citric_acid 80\n", "residual_sugar 91\n", "chlorides 153\n", "free_sulfur_dioxide 60\n", "total_sulfur_dioxide 144\n", "density 436\n", "pH_5 89\n", "sulphates 96\n", "alcohol 65\n", "quality_6 6\n", "dtype: int64" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.nunique()" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [], "source": [ "wine_df = wine_df.drop_duplicates(subset='density')" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(436, 12)" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.shape #436 unique values in the density column " ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [], "source": [ "wine_df.set_index('density',inplace=True)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidepH_5sulphatesalcoholquality_6
density
0.99787.40.700.001.90.07611.034.03.510.569.45
0.99687.80.880.002.60.09825.067.03.200.689.85
0.99707.80.760.042.30.09215.054.03.260.659.85
0.998011.20.280.561.90.07517.060.03.160.589.86
0.99647.90.600.061.60.06915.059.03.300.469.45
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar \\\n", "density \n", "0.9978 7.4 0.70 0.00 1.9 \n", "0.9968 7.8 0.88 0.00 2.6 \n", "0.9970 7.8 0.76 0.04 2.3 \n", "0.9980 11.2 0.28 0.56 1.9 \n", "0.9964 7.9 0.60 0.06 1.6 \n", "\n", " chlorides free_sulfur_dioxide total_sulfur_dioxide pH_5 \\\n", "density \n", "0.9978 0.076 11.0 34.0 3.51 \n", "0.9968 0.098 25.0 67.0 3.20 \n", "0.9970 0.092 15.0 54.0 3.26 \n", "0.9980 0.075 17.0 60.0 3.16 \n", "0.9964 0.069 15.0 59.0 3.30 \n", "\n", " sulphates alcohol quality_6 \n", "density \n", "0.9978 0.56 9.4 5 \n", "0.9968 0.68 9.8 5 \n", "0.9970 0.65 9.8 5 \n", "0.9980 0.58 9.8 6 \n", "0.9964 0.46 9.4 5 " ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.head()" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "fixed_acidity 7.800\n", "volatile_acidity 0.760\n", "citric_acid 0.040\n", "residual_sugar 2.300\n", "chlorides 0.092\n", "free_sulfur_dioxide 15.000\n", "total_sulfur_dioxide 54.000\n", "pH_5 3.260\n", "sulphates 0.650\n", "alcohol 9.800\n", "quality_6 5.000\n", "Name: 0.997, dtype: float64" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.iloc[2]" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "fixed_acidity 7.800\n", "volatile_acidity 0.880\n", "citric_acid 0.000\n", "residual_sugar 2.600\n", "chlorides 0.098\n", "free_sulfur_dioxide 25.000\n", "total_sulfur_dioxide 67.000\n", "pH_5 3.200\n", "sulphates 0.680\n", "alcohol 9.800\n", "quality_6 5.000\n", "Name: 0.9968, dtype: float64" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.loc[0.9968] " ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidepH_5sulphatesalcoholquality_6
density
0.99687.80.8800.002.60.09825.067.03.200.689.85
0.99647.90.6000.061.60.06915.059.03.300.469.45
0.99435.60.6150.001.60.08916.059.03.580.529.95
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar \\\n", "density \n", "0.9968 7.8 0.880 0.00 2.6 \n", "0.9964 7.9 0.600 0.06 1.6 \n", "0.9943 5.6 0.615 0.00 1.6 \n", "\n", " chlorides free_sulfur_dioxide total_sulfur_dioxide pH_5 \\\n", "density \n", "0.9968 0.098 25.0 67.0 3.20 \n", "0.9964 0.069 15.0 59.0 3.30 \n", "0.9943 0.089 16.0 59.0 3.58 \n", "\n", " sulphates alcohol quality_6 \n", "density \n", "0.9968 0.68 9.8 5 \n", "0.9964 0.46 9.4 5 \n", "0.9943 0.52 9.9 5 " ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.iloc[[1, 4, 7]] " ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidepH_5sulphatesalcoholquality_6
density
0.99687.80.8800.002.60.09825.067.03.200.689.85
0.99647.90.6000.061.60.06915.059.03.300.469.45
0.99435.60.6150.001.60.08916.059.03.580.529.95
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar \\\n", "density \n", "0.9968 7.8 0.880 0.00 2.6 \n", "0.9964 7.9 0.600 0.06 1.6 \n", "0.9943 5.6 0.615 0.00 1.6 \n", "\n", " chlorides free_sulfur_dioxide total_sulfur_dioxide pH_5 \\\n", "density \n", "0.9968 0.098 25.0 67.0 3.20 \n", "0.9964 0.069 15.0 59.0 3.30 \n", "0.9943 0.089 16.0 59.0 3.58 \n", "\n", " sulphates alcohol quality_6 \n", "density \n", "0.9968 0.68 9.8 5 \n", "0.9964 0.46 9.4 5 \n", "0.9943 0.52 9.9 5 " ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rows = [0.9968, 0.9964, 0.9943]\n", "wine_df.loc[rows]" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidepH_5sulphatesalcoholquality_6
density
0.99687.80.880.002.60.09825.067.03.200.689.85
0.99707.80.760.042.30.09215.054.03.260.659.85
0.998011.20.280.561.90.07517.060.03.160.589.86
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar \\\n", "density \n", "0.9968 7.8 0.88 0.00 2.6 \n", "0.9970 7.8 0.76 0.04 2.3 \n", "0.9980 11.2 0.28 0.56 1.9 \n", "\n", " chlorides free_sulfur_dioxide total_sulfur_dioxide pH_5 \\\n", "density \n", "0.9968 0.098 25.0 67.0 3.20 \n", "0.9970 0.092 15.0 54.0 3.26 \n", "0.9980 0.075 17.0 60.0 3.16 \n", "\n", " sulphates alcohol quality_6 \n", "density \n", "0.9968 0.68 9.8 5 \n", "0.9970 0.65 9.8 5 \n", "0.9980 0.58 9.8 6 " ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.iloc[1:4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slice notation also works with the .loc indexer and is inclusive of the last label:" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidepH_5sulphatesalcoholquality_6
density
0.99707.80.760.042.30.09215.054.03.260.659.85
0.998011.20.280.561.90.07517.060.03.160.589.86
0.99647.90.600.061.60.06915.059.03.300.469.45
0.99467.30.650.001.20.06515.021.03.390.4710.07
0.99596.70.580.081.80.09715.065.03.280.549.25
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar \\\n", "density \n", "0.9970 7.8 0.76 0.04 2.3 \n", "0.9980 11.2 0.28 0.56 1.9 \n", "0.9964 7.9 0.60 0.06 1.6 \n", "0.9946 7.3 0.65 0.00 1.2 \n", "0.9959 6.7 0.58 0.08 1.8 \n", "\n", " chlorides free_sulfur_dioxide total_sulfur_dioxide pH_5 \\\n", "density \n", "0.9970 0.092 15.0 54.0 3.26 \n", "0.9980 0.075 17.0 60.0 3.16 \n", "0.9964 0.069 15.0 59.0 3.30 \n", "0.9946 0.065 15.0 21.0 3.39 \n", "0.9959 0.097 15.0 65.0 3.28 \n", "\n", " sulphates alcohol quality_6 \n", "density \n", "0.9970 0.65 9.8 5 \n", "0.9980 0.58 9.8 6 \n", "0.9964 0.46 9.4 5 \n", "0.9946 0.47 10.0 7 \n", "0.9959 0.54 9.2 5 " ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first = 0.9970\n", "last = 0.9959\n", "wine_df.loc[first:last]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selecting rows and columns simultaneously" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidepH_5sulphatesalcoholquality_6
density
0.99787.40.700.001.90.07611.034.03.510.569.45
0.99687.80.880.002.60.09825.067.03.200.689.85
0.99707.80.760.042.30.09215.054.03.260.659.85
0.998011.20.280.561.90.07517.060.03.160.589.86
0.99647.90.600.061.60.06915.059.03.300.469.45
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar \\\n", "density \n", "0.9978 7.4 0.70 0.00 1.9 \n", "0.9968 7.8 0.88 0.00 2.6 \n", "0.9970 7.8 0.76 0.04 2.3 \n", "0.9980 11.2 0.28 0.56 1.9 \n", "0.9964 7.9 0.60 0.06 1.6 \n", "\n", " chlorides free_sulfur_dioxide total_sulfur_dioxide pH_5 \\\n", "density \n", "0.9978 0.076 11.0 34.0 3.51 \n", "0.9968 0.098 25.0 67.0 3.20 \n", "0.9970 0.092 15.0 54.0 3.26 \n", "0.9980 0.075 17.0 60.0 3.16 \n", "0.9964 0.069 15.0 59.0 3.30 \n", "\n", " sulphates alcohol quality_6 \n", "density \n", "0.9978 0.56 9.4 5 \n", "0.9968 0.68 9.8 5 \n", "0.9970 0.65 9.8 5 \n", "0.9980 0.58 9.8 6 \n", "0.9964 0.46 9.4 5 " ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.head()" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
residual_sugarchloridestotal_sulfur_dioxide
density
0.99781.90.07634.0
0.99682.60.09867.0
0.99702.30.09254.0
0.99801.90.07560.0
0.99641.60.06959.0
\n", "
" ], "text/plain": [ " residual_sugar chlorides total_sulfur_dioxide\n", "density \n", "0.9978 1.9 0.076 34.0\n", "0.9968 2.6 0.098 67.0\n", "0.9970 2.3 0.092 54.0\n", "0.9980 1.9 0.075 60.0\n", "0.9964 1.6 0.069 59.0" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.iloc[:, [3,4,6]].head()" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
residual_sugarchloridestotal_sulfur_dioxide
density
0.99781.90.07634.0
0.99682.60.09867.0
0.99702.30.09254.0
0.99801.90.07560.0
0.99641.60.06959.0
\n", "
" ], "text/plain": [ " residual_sugar chlorides total_sulfur_dioxide\n", "density \n", "0.9978 1.9 0.076 34.0\n", "0.9968 2.6 0.098 67.0\n", "0.9970 2.3 0.092 54.0\n", "0.9980 1.9 0.075 60.0\n", "0.9964 1.6 0.069 59.0" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.loc[:,['residual_sugar','chlorides','total_sulfur_dioxide']].head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selecting disjointed rows and columns \n", "\n" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "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", "
pH_5alcohol
density
0.99693.3010.5
0.99623.289.5
\n", "
" ], "text/plain": [ " pH_5 alcohol\n", "density \n", "0.9969 3.30 10.5\n", "0.9962 3.28 9.5" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.iloc[[10,14], [7, 9]]" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "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", "
pH_5alcohol
density
0.99693.3010.5
0.99623.289.5
\n", "
" ], "text/plain": [ " pH_5 alcohol\n", "density \n", "0.9969 3.30 10.5\n", "0.9962 3.28 9.5" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rows = [0.9969, 0.9962]\n", "columns = ['pH_5', 'alcohol']\n", "wine_df.loc[rows,columns]" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.092" ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.loc[0.9970, 'chlorides']" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.092" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.iloc[2, 4]\n" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "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", " \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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidepH_5sulphatesalcoholquality_6
density
0.99787.40.7000.001.90.07611.034.03.510.569.45
0.99687.80.8800.002.60.09825.067.03.200.689.85
0.99707.80.7600.042.30.09215.054.03.260.659.85
0.998011.20.2800.561.90.07517.060.03.160.589.86
0.99647.90.6000.061.60.06915.059.03.300.469.45
0.99467.30.6500.001.20.06515.021.03.390.4710.07
0.99596.70.5800.081.80.09715.065.03.280.549.25
0.99435.60.6150.001.60.08916.059.03.580.529.95
0.99747.80.6100.291.60.1149.029.03.261.569.15
0.99868.90.6200.183.80.17652.0145.03.160.889.25
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar \\\n", "density \n", "0.9978 7.4 0.700 0.00 1.9 \n", "0.9968 7.8 0.880 0.00 2.6 \n", "0.9970 7.8 0.760 0.04 2.3 \n", "0.9980 11.2 0.280 0.56 1.9 \n", "0.9964 7.9 0.600 0.06 1.6 \n", "0.9946 7.3 0.650 0.00 1.2 \n", "0.9959 6.7 0.580 0.08 1.8 \n", "0.9943 5.6 0.615 0.00 1.6 \n", "0.9974 7.8 0.610 0.29 1.6 \n", "0.9986 8.9 0.620 0.18 3.8 \n", "\n", " chlorides free_sulfur_dioxide total_sulfur_dioxide pH_5 \\\n", "density \n", "0.9978 0.076 11.0 34.0 3.51 \n", "0.9968 0.098 25.0 67.0 3.20 \n", "0.9970 0.092 15.0 54.0 3.26 \n", "0.9980 0.075 17.0 60.0 3.16 \n", "0.9964 0.069 15.0 59.0 3.30 \n", "0.9946 0.065 15.0 21.0 3.39 \n", "0.9959 0.097 15.0 65.0 3.28 \n", "0.9943 0.089 16.0 59.0 3.58 \n", "0.9974 0.114 9.0 29.0 3.26 \n", "0.9986 0.176 52.0 145.0 3.16 \n", "\n", " sulphates alcohol quality_6 \n", "density \n", "0.9978 0.56 9.4 5 \n", "0.9968 0.68 9.8 5 \n", "0.9970 0.65 9.8 5 \n", "0.9980 0.58 9.8 6 \n", "0.9964 0.46 9.4 5 \n", "0.9946 0.47 10.0 7 \n", "0.9959 0.54 9.2 5 \n", "0.9943 0.52 9.9 5 \n", "0.9974 1.56 9.1 5 \n", "0.9986 0.88 9.2 5 " ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.head(10)" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "density\n", "0.9974 0.29\n", "0.9959 0.08\n", "0.9964 0.06\n", "Name: citric_acid, dtype: float64" ] }, "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.iloc[8:2:-2, 2]" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "density\n", "0.9974 0.29\n", "0.9959 0.08\n", "0.9964 0.06\n", "Name: citric_acid, dtype: float64" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first = 0.9974\n", "second = 0.9964\n", "\n", "wine_df.loc[first:second:-2, 'citric_acid']" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidepH_5sulphatesalcoholquality_6
density
0.99787.40.700.001.90.07611.034.03.510.569.45
0.99687.80.880.002.60.09825.067.03.200.689.85
0.99707.80.760.042.30.09215.054.03.260.659.85
0.998011.20.280.561.90.07517.060.03.160.589.86
0.99647.90.600.061.60.06915.059.03.300.469.45
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar \\\n", "density \n", "0.9978 7.4 0.70 0.00 1.9 \n", "0.9968 7.8 0.88 0.00 2.6 \n", "0.9970 7.8 0.76 0.04 2.3 \n", "0.9980 11.2 0.28 0.56 1.9 \n", "0.9964 7.9 0.60 0.06 1.6 \n", "\n", " chlorides free_sulfur_dioxide total_sulfur_dioxide pH_5 \\\n", "density \n", "0.9978 0.076 11.0 34.0 3.51 \n", "0.9968 0.098 25.0 67.0 3.20 \n", "0.9970 0.092 15.0 54.0 3.26 \n", "0.9980 0.075 17.0 60.0 3.16 \n", "0.9964 0.069 15.0 59.0 3.30 \n", "\n", " sulphates alcohol quality_6 \n", "density \n", "0.9978 0.56 9.4 5 \n", "0.9968 0.68 9.8 5 \n", "0.9970 0.65 9.8 5 \n", "0.9980 0.58 9.8 6 \n", "0.9964 0.46 9.4 5 " ] }, "execution_count": 165, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.iloc[:5]" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "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", " \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", "
fixed_acidityvolatile_aciditycitric_acidresidual_sugarchloridesfree_sulfur_dioxidetotal_sulfur_dioxidepH_5sulphatesalcoholquality_6
density
0.99787.40.700.001.90.07611.034.03.510.569.45
0.99687.80.880.002.60.09825.067.03.200.689.85
0.99707.80.760.042.30.09215.054.03.260.659.85
0.998011.20.280.561.90.07517.060.03.160.589.86
0.99647.90.600.061.60.06915.059.03.300.469.45
\n", "
" ], "text/plain": [ " fixed_acidity volatile_acidity citric_acid residual_sugar \\\n", "density \n", "0.9978 7.4 0.70 0.00 1.9 \n", "0.9968 7.8 0.88 0.00 2.6 \n", "0.9970 7.8 0.76 0.04 2.3 \n", "0.9980 11.2 0.28 0.56 1.9 \n", "0.9964 7.9 0.60 0.06 1.6 \n", "\n", " chlorides free_sulfur_dioxide total_sulfur_dioxide pH_5 \\\n", "density \n", "0.9978 0.076 11.0 34.0 3.51 \n", "0.9968 0.098 25.0 67.0 3.20 \n", "0.9970 0.092 15.0 54.0 3.26 \n", "0.9980 0.075 17.0 60.0 3.16 \n", "0.9964 0.069 15.0 59.0 3.30 \n", "\n", " sulphates alcohol quality_6 \n", "density \n", "0.9978 0.56 9.4 5 \n", "0.9968 0.68 9.8 5 \n", "0.9970 0.65 9.8 5 \n", "0.9980 0.58 9.8 6 \n", "0.9964 0.46 9.4 5 " ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.iloc[:5, :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selecting rows and columns using get_loc and index methods\n" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 3)" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "col_start = wine_df.columns.get_loc('volatile_acidity')\n", "col_end = wine_df.columns.get_loc('volatile_acidity')+2\n", "col_start, col_end" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "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", "
volatile_aciditycitric_acid
density
0.99780.700.00
0.99680.880.00
0.99700.760.04
0.99800.280.56
\n", "
" ], "text/plain": [ " volatile_acidity citric_acid\n", "density \n", "0.9978 0.70 0.00\n", "0.9968 0.88 0.00\n", "0.9970 0.76 0.04\n", "0.9980 0.28 0.56" ] }, "execution_count": 177, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.iloc[:4, col_start:col_end]" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
volatile_aciditycitric_acidresidual_sugarchlorides
density
0.99640.6000.061.60.069
0.99460.6500.001.20.065
0.99590.5800.081.80.097
0.99430.6150.001.60.089
\n", "
" ], "text/plain": [ " volatile_acidity citric_acid residual_sugar chlorides\n", "density \n", "0.9964 0.600 0.06 1.6 0.069\n", "0.9946 0.650 0.00 1.2 0.065\n", "0.9959 0.580 0.08 1.8 0.097\n", "0.9943 0.615 0.00 1.6 0.089" ] }, "execution_count": 196, "metadata": {}, "output_type": "execute_result" } ], "source": [ "row_start = wine_df.index[4]\n", "row_end = wine_df.index[7]\n", "\n", "wine_df.loc[row_start:row_end, 'volatile_acidity' : 'chlorides']" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.56" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "density_val = 0.9980\n", "wine_df.loc[density_val, 'citric_acid']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selecting rows and columns using .iat and .at\n" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.56" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wine_df.at[density_val, 'citric_acid']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " The %timeit magic command to find the difference in speed:" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9.77 µs ± 633 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "timeit wine_df.loc[density_val, 'citric_acid'] " ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6.2 µs ± 246 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "timeit wine_df.at[density_val, 'citric_acid']" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 2)" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "row_num = wine_df.index.get_loc(density_val)\n", "col_num = wine_df.columns.get_loc('citric_acid')\n", "row_num,col_num" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11 µs ± 506 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "timeit wine_df.iloc[row_num, col_num]" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.81 µs ± 1.39 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "timeit wine_df.iat[row_num, col_num]" ] } ], "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }