{
"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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 7.9 | \n",
" 0.60 | \n",
" 0.06 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 15.0 | \n",
" 59.0 | \n",
" 3.30 | \n",
" 0.46 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
"
\n",
" \n",
" 1 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
"
\n",
" \n",
" 2 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
"
\n",
" \n",
" 3 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
"
\n",
" \n",
" 4 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" density | \n",
" pH | \n",
" sulphates | \n",
" alcohol | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 0.9978 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
"
\n",
" \n",
" 1 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 0.9968 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
"
\n",
" \n",
" 2 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 0.9970 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
"
\n",
" \n",
" 3 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 0.9980 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
"
\n",
" \n",
" 4 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 0.9978 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" density | \n",
" pH | \n",
" sulphates | \n",
" alcohol | \n",
" quality | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 0.9978 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 1 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 0.9968 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 2 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 0.9970 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 3 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 0.9980 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
" 4 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 0.9978 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
"
\n",
" \n",
" 1 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
"
\n",
" \n",
" 2 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
"
\n",
" \n",
" 3 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
"
\n",
" \n",
" 4 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
"
\n",
" \n",
"
\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",
" pH_5 | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 3.51 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 3.20 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 3.26 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 3.16 | \n",
" 6 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 3.30 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" density | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 0.9978 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 1 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 0.9968 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 2 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 0.9970 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 3 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 0.9980 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
" 4 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 0.9978 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 7.4 | \n",
" 0.70 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.88 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 7.8 | \n",
" 0.76 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 11.2 | \n",
" 0.28 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 7.9 | \n",
" 0.60 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" density | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 0.9978 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 1 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 0.9968 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 2 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 0.9970 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 3 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 0.9980 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
" 4 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 0.9978 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" density | \n",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 0.9978 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
"
\n",
" \n",
" 1 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
"
\n",
" \n",
" 2 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 0.9970 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
"
\n",
" \n",
" 3 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 0.9980 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
"
\n",
" \n",
" 4 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 0.9978 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 7.9 | \n",
" 0.60 | \n",
" 0.06 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 15.0 | \n",
" 59.0 | \n",
" 3.30 | \n",
" 0.46 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.880 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 7.9 | \n",
" 0.600 | \n",
" 0.06 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 15.0 | \n",
" 59.0 | \n",
" 3.30 | \n",
" 0.46 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9943 | \n",
" 5.6 | \n",
" 0.615 | \n",
" 0.00 | \n",
" 1.6 | \n",
" 0.089 | \n",
" 16.0 | \n",
" 59.0 | \n",
" 3.58 | \n",
" 0.52 | \n",
" 9.9 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.880 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 7.9 | \n",
" 0.600 | \n",
" 0.06 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 15.0 | \n",
" 59.0 | \n",
" 3.30 | \n",
" 0.46 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9943 | \n",
" 5.6 | \n",
" 0.615 | \n",
" 0.00 | \n",
" 1.6 | \n",
" 0.089 | \n",
" 16.0 | \n",
" 59.0 | \n",
" 3.58 | \n",
" 0.52 | \n",
" 9.9 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9970 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 7.9 | \n",
" 0.60 | \n",
" 0.06 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 15.0 | \n",
" 59.0 | \n",
" 3.30 | \n",
" 0.46 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9946 | \n",
" 7.3 | \n",
" 0.65 | \n",
" 0.00 | \n",
" 1.2 | \n",
" 0.065 | \n",
" 15.0 | \n",
" 21.0 | \n",
" 3.39 | \n",
" 0.47 | \n",
" 10.0 | \n",
" 7 | \n",
"
\n",
" \n",
" 0.9959 | \n",
" 6.7 | \n",
" 0.58 | \n",
" 0.08 | \n",
" 1.8 | \n",
" 0.097 | \n",
" 15.0 | \n",
" 65.0 | \n",
" 3.28 | \n",
" 0.54 | \n",
" 9.2 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 7.9 | \n",
" 0.60 | \n",
" 0.06 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 15.0 | \n",
" 59.0 | \n",
" 3.30 | \n",
" 0.46 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" residual_sugar | \n",
" chlorides | \n",
" total_sulfur_dioxide | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 34.0 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 67.0 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 54.0 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 60.0 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 59.0 | \n",
"
\n",
" \n",
"
\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",
" residual_sugar | \n",
" chlorides | \n",
" total_sulfur_dioxide | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 34.0 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 67.0 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 54.0 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 60.0 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 59.0 | \n",
"
\n",
" \n",
"
\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",
" pH_5 | \n",
" alcohol | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9969 | \n",
" 3.30 | \n",
" 10.5 | \n",
"
\n",
" \n",
" 0.9962 | \n",
" 3.28 | \n",
" 9.5 | \n",
"
\n",
" \n",
"
\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",
" pH_5 | \n",
" alcohol | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9969 | \n",
" 3.30 | \n",
" 10.5 | \n",
"
\n",
" \n",
" 0.9962 | \n",
" 3.28 | \n",
" 9.5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 7.4 | \n",
" 0.700 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.880 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 7.8 | \n",
" 0.760 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 11.2 | \n",
" 0.280 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 7.9 | \n",
" 0.600 | \n",
" 0.06 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 15.0 | \n",
" 59.0 | \n",
" 3.30 | \n",
" 0.46 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9946 | \n",
" 7.3 | \n",
" 0.650 | \n",
" 0.00 | \n",
" 1.2 | \n",
" 0.065 | \n",
" 15.0 | \n",
" 21.0 | \n",
" 3.39 | \n",
" 0.47 | \n",
" 10.0 | \n",
" 7 | \n",
"
\n",
" \n",
" 0.9959 | \n",
" 6.7 | \n",
" 0.580 | \n",
" 0.08 | \n",
" 1.8 | \n",
" 0.097 | \n",
" 15.0 | \n",
" 65.0 | \n",
" 3.28 | \n",
" 0.54 | \n",
" 9.2 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9943 | \n",
" 5.6 | \n",
" 0.615 | \n",
" 0.00 | \n",
" 1.6 | \n",
" 0.089 | \n",
" 16.0 | \n",
" 59.0 | \n",
" 3.58 | \n",
" 0.52 | \n",
" 9.9 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9974 | \n",
" 7.8 | \n",
" 0.610 | \n",
" 0.29 | \n",
" 1.6 | \n",
" 0.114 | \n",
" 9.0 | \n",
" 29.0 | \n",
" 3.26 | \n",
" 1.56 | \n",
" 9.1 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9986 | \n",
" 8.9 | \n",
" 0.620 | \n",
" 0.18 | \n",
" 3.8 | \n",
" 0.176 | \n",
" 52.0 | \n",
" 145.0 | \n",
" 3.16 | \n",
" 0.88 | \n",
" 9.2 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 7.9 | \n",
" 0.60 | \n",
" 0.06 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 15.0 | \n",
" 59.0 | \n",
" 3.30 | \n",
" 0.46 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" fixed_acidity | \n",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
" free_sulfur_dioxide | \n",
" total_sulfur_dioxide | \n",
" pH_5 | \n",
" sulphates | \n",
" alcohol | \n",
" quality_6 | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 7.4 | \n",
" 0.70 | \n",
" 0.00 | \n",
" 1.9 | \n",
" 0.076 | \n",
" 11.0 | \n",
" 34.0 | \n",
" 3.51 | \n",
" 0.56 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 7.8 | \n",
" 0.88 | \n",
" 0.00 | \n",
" 2.6 | \n",
" 0.098 | \n",
" 25.0 | \n",
" 67.0 | \n",
" 3.20 | \n",
" 0.68 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 7.8 | \n",
" 0.76 | \n",
" 0.04 | \n",
" 2.3 | \n",
" 0.092 | \n",
" 15.0 | \n",
" 54.0 | \n",
" 3.26 | \n",
" 0.65 | \n",
" 9.8 | \n",
" 5 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 11.2 | \n",
" 0.28 | \n",
" 0.56 | \n",
" 1.9 | \n",
" 0.075 | \n",
" 17.0 | \n",
" 60.0 | \n",
" 3.16 | \n",
" 0.58 | \n",
" 9.8 | \n",
" 6 | \n",
"
\n",
" \n",
" 0.9964 | \n",
" 7.9 | \n",
" 0.60 | \n",
" 0.06 | \n",
" 1.6 | \n",
" 0.069 | \n",
" 15.0 | \n",
" 59.0 | \n",
" 3.30 | \n",
" 0.46 | \n",
" 9.4 | \n",
" 5 | \n",
"
\n",
" \n",
"
\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",
" volatile_acidity | \n",
" citric_acid | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9978 | \n",
" 0.70 | \n",
" 0.00 | \n",
"
\n",
" \n",
" 0.9968 | \n",
" 0.88 | \n",
" 0.00 | \n",
"
\n",
" \n",
" 0.9970 | \n",
" 0.76 | \n",
" 0.04 | \n",
"
\n",
" \n",
" 0.9980 | \n",
" 0.28 | \n",
" 0.56 | \n",
"
\n",
" \n",
"
\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",
" volatile_acidity | \n",
" citric_acid | \n",
" residual_sugar | \n",
" chlorides | \n",
"
\n",
" \n",
" density | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0.9964 | \n",
" 0.600 | \n",
" 0.06 | \n",
" 1.6 | \n",
" 0.069 | \n",
"
\n",
" \n",
" 0.9946 | \n",
" 0.650 | \n",
" 0.00 | \n",
" 1.2 | \n",
" 0.065 | \n",
"
\n",
" \n",
" 0.9959 | \n",
" 0.580 | \n",
" 0.08 | \n",
" 1.8 | \n",
" 0.097 | \n",
"
\n",
" \n",
" 0.9943 | \n",
" 0.615 | \n",
" 0.00 | \n",
" 1.6 | \n",
" 0.089 | \n",
"
\n",
" \n",
"
\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
}