{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "\n", "\n", "___\n", "
Copyright by Pierian Data Inc.
\n", "
For more information, visit us at www.pieriandata.com
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Groupby Operations and Multi-level Index" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('mpg.csv')" ] }, { "cell_type": "code", "execution_count": 3, "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", "
mpgcylindersdisplacementhorsepowerweightaccelerationmodel_yearoriginname
018.08307.0130350412.0701chevrolet chevelle malibu
115.08350.0165369311.5701buick skylark 320
218.08318.0150343611.0701plymouth satellite
316.08304.0150343312.0701amc rebel sst
417.08302.0140344910.5701ford torino
..............................
39327.04140.086279015.6821ford mustang gl
39444.0497.052213024.6822vw pickup
39532.04135.084229511.6821dodge rampage
39628.04120.079262518.6821ford ranger
39731.04119.082272019.4821chevy s-10
\n", "

398 rows × 9 columns

\n", "
" ], "text/plain": [ " mpg cylinders displacement horsepower weight acceleration \\\n", "0 18.0 8 307.0 130 3504 12.0 \n", "1 15.0 8 350.0 165 3693 11.5 \n", "2 18.0 8 318.0 150 3436 11.0 \n", "3 16.0 8 304.0 150 3433 12.0 \n", "4 17.0 8 302.0 140 3449 10.5 \n", ".. ... ... ... ... ... ... \n", "393 27.0 4 140.0 86 2790 15.6 \n", "394 44.0 4 97.0 52 2130 24.6 \n", "395 32.0 4 135.0 84 2295 11.6 \n", "396 28.0 4 120.0 79 2625 18.6 \n", "397 31.0 4 119.0 82 2720 19.4 \n", "\n", " model_year origin name \n", "0 70 1 chevrolet chevelle malibu \n", "1 70 1 buick skylark 320 \n", "2 70 1 plymouth satellite \n", "3 70 1 amc rebel sst \n", "4 70 1 ford torino \n", ".. ... ... ... \n", "393 82 1 ford mustang gl \n", "394 82 2 vw pickup \n", "395 82 1 dodge rampage \n", "396 82 1 ford ranger \n", "397 82 1 chevy s-10 \n", "\n", "[398 rows x 9 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## groupby() method" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Creates a groupby object waiting for an aggregate method\n", "df.groupby('model_year')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Adding an aggregate method call. To use a grouped object, you need to tell pandas how you want to aggregate the data.\n", "\n", "Common Options:\n", "\n", " mean(): Compute mean of groups\n", " sum(): Compute sum of group values\n", " size(): Compute group sizes\n", " count(): Compute count of group\n", " std(): Standard deviation of groups\n", " var(): Compute variance of groups\n", " sem(): Standard error of the mean of groups\n", " describe(): Generates descriptive statistics\n", " first(): Compute first of group values\n", " last(): Compute last of group values\n", " nth() : Take nth value, or a subset if n is a list\n", " min(): Compute min of group values\n", " max(): Compute max of group values\n", " \n", "Full List at the Online Documentation: https://pandas.pydata.org/docs/reference/groupby.html" ] }, { "cell_type": "code", "execution_count": 5, "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", "
mpgcylindersdisplacementweightaccelerationorigin
model_year
7017.6896556.758621281.4137933372.79310312.9482761.310345
7121.2500005.571429209.7500002995.42857115.1428571.428571
7218.7142865.821429218.3750003237.71428615.1250001.535714
7317.1000006.375000256.8750003419.02500014.3125001.375000
7422.7037045.259259171.7407412877.92592616.2037041.666667
7520.2666675.600000205.5333333176.80000016.0500001.466667
7621.5735295.647059197.7941183078.73529415.9411761.470588
7723.3750005.464286191.3928572997.35714315.4357141.571429
7824.0611115.361111177.8055562861.80555615.8055561.611111
7925.0931035.827586206.6896553055.34482815.8137931.275862
8033.6965524.137931115.8275862436.65517216.9344832.206897
8130.3344834.620690135.3103452522.93103416.3068971.965517
8231.7096774.193548128.8709682453.54838716.6387101.645161
\n", "
" ], "text/plain": [ " mpg cylinders displacement weight acceleration \\\n", "model_year \n", "70 17.689655 6.758621 281.413793 3372.793103 12.948276 \n", "71 21.250000 5.571429 209.750000 2995.428571 15.142857 \n", "72 18.714286 5.821429 218.375000 3237.714286 15.125000 \n", "73 17.100000 6.375000 256.875000 3419.025000 14.312500 \n", "74 22.703704 5.259259 171.740741 2877.925926 16.203704 \n", "75 20.266667 5.600000 205.533333 3176.800000 16.050000 \n", "76 21.573529 5.647059 197.794118 3078.735294 15.941176 \n", "77 23.375000 5.464286 191.392857 2997.357143 15.435714 \n", "78 24.061111 5.361111 177.805556 2861.805556 15.805556 \n", "79 25.093103 5.827586 206.689655 3055.344828 15.813793 \n", "80 33.696552 4.137931 115.827586 2436.655172 16.934483 \n", "81 30.334483 4.620690 135.310345 2522.931034 16.306897 \n", "82 31.709677 4.193548 128.870968 2453.548387 16.638710 \n", "\n", " origin \n", "model_year \n", "70 1.310345 \n", "71 1.428571 \n", "72 1.535714 \n", "73 1.375000 \n", "74 1.666667 \n", "75 1.466667 \n", "76 1.470588 \n", "77 1.571429 \n", "78 1.611111 \n", "79 1.275862 \n", "80 2.206897 \n", "81 1.965517 \n", "82 1.645161 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# model_year becomes the index! It is NOT a column name,it is now the name of the index\n", "df.groupby('model_year').mean()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "avg_year = df.groupby('model_year').mean()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Int64Index([70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82], dtype='int64', name='model_year')" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "avg_year.index" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['mpg', 'cylinders', 'displacement', 'weight', 'acceleration', 'origin'], dtype='object')" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "avg_year.columns" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "model_year\n", "70 17.689655\n", "71 21.250000\n", "72 18.714286\n", "73 17.100000\n", "74 22.703704\n", "75 20.266667\n", "76 21.573529\n", "77 23.375000\n", "78 24.061111\n", "79 25.093103\n", "80 33.696552\n", "81 30.334483\n", "82 31.709677\n", "Name: mpg, dtype: float64" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "avg_year['mpg']" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "model_year\n", "70 17.689655\n", "71 21.250000\n", "72 18.714286\n", "73 17.100000\n", "74 22.703704\n", "75 20.266667\n", "76 21.573529\n", "77 23.375000\n", "78 24.061111\n", "79 25.093103\n", "80 33.696552\n", "81 30.334483\n", "82 31.709677\n", "Name: mpg, dtype: float64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.groupby('model_year').mean()['mpg']" ] }, { "cell_type": "code", "execution_count": 11, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mpgcylinders...accelerationorigin
countmeanstdmin25%50%75%maxcountmean...75%maxcountmeanstdmin25%50%75%max
model_year
7029.017.6896555.3392319.014.00016.0022.00027.029.06.758621...15.00020.529.01.3103450.6037651.01.01.01.03.0
7128.021.2500006.59194212.015.50019.0027.00035.028.05.571429...16.12520.528.01.4285710.7417981.01.01.02.03.0
7228.018.7142865.43552911.013.75018.5023.00028.028.05.821429...16.62523.528.01.5357140.7926581.01.01.02.03.0
7340.017.1000004.70024511.013.00016.0020.00029.040.06.375000...16.00021.040.01.3750000.6674671.01.01.02.03.0
7427.022.7037046.42001013.016.00024.0027.00032.027.05.259259...17.00021.027.01.6666670.8320501.01.01.02.03.0
7530.020.2666674.94056613.016.00019.5023.00033.030.05.600000...17.37521.030.01.4666670.7302971.01.01.02.03.0
7634.021.5735295.88929713.016.75021.0026.37533.034.05.647059...17.55022.234.01.4705880.7064761.01.01.02.03.0
7728.023.3750006.67586215.017.37521.7530.00036.028.05.464286...16.92519.028.01.5714290.8357111.01.01.02.03.0
7836.024.0611116.89804416.219.35020.7028.00043.136.05.361111...16.82521.536.01.6111110.8376081.01.01.02.03.0
7929.025.0931036.79421715.519.20023.9031.80037.329.05.827586...17.30024.829.01.2758620.5914001.01.01.01.03.0
8029.033.6965527.03798319.129.80032.7038.10046.629.04.137931...18.70023.729.02.2068970.8185051.02.02.03.03.0
8129.030.3344835.59146517.626.60031.6034.40039.129.04.620690...17.30020.729.01.9655170.9442591.01.02.03.03.0
8231.031.7096775.39254822.027.00032.0036.00044.031.04.193548...18.00024.631.01.6451610.9146361.01.01.03.03.0
\n", "

13 rows × 48 columns

\n", "
" ], "text/plain": [ " mpg \\\n", " count mean std min 25% 50% 75% max \n", "model_year \n", "70 29.0 17.689655 5.339231 9.0 14.000 16.00 22.000 27.0 \n", "71 28.0 21.250000 6.591942 12.0 15.500 19.00 27.000 35.0 \n", "72 28.0 18.714286 5.435529 11.0 13.750 18.50 23.000 28.0 \n", "73 40.0 17.100000 4.700245 11.0 13.000 16.00 20.000 29.0 \n", "74 27.0 22.703704 6.420010 13.0 16.000 24.00 27.000 32.0 \n", "75 30.0 20.266667 4.940566 13.0 16.000 19.50 23.000 33.0 \n", "76 34.0 21.573529 5.889297 13.0 16.750 21.00 26.375 33.0 \n", "77 28.0 23.375000 6.675862 15.0 17.375 21.75 30.000 36.0 \n", "78 36.0 24.061111 6.898044 16.2 19.350 20.70 28.000 43.1 \n", "79 29.0 25.093103 6.794217 15.5 19.200 23.90 31.800 37.3 \n", "80 29.0 33.696552 7.037983 19.1 29.800 32.70 38.100 46.6 \n", "81 29.0 30.334483 5.591465 17.6 26.600 31.60 34.400 39.1 \n", "82 31.0 31.709677 5.392548 22.0 27.000 32.00 36.000 44.0 \n", "\n", " cylinders ... acceleration origin \\\n", " count mean ... 75% max count mean \n", "model_year ... \n", "70 29.0 6.758621 ... 15.000 20.5 29.0 1.310345 \n", "71 28.0 5.571429 ... 16.125 20.5 28.0 1.428571 \n", "72 28.0 5.821429 ... 16.625 23.5 28.0 1.535714 \n", "73 40.0 6.375000 ... 16.000 21.0 40.0 1.375000 \n", "74 27.0 5.259259 ... 17.000 21.0 27.0 1.666667 \n", "75 30.0 5.600000 ... 17.375 21.0 30.0 1.466667 \n", "76 34.0 5.647059 ... 17.550 22.2 34.0 1.470588 \n", "77 28.0 5.464286 ... 16.925 19.0 28.0 1.571429 \n", "78 36.0 5.361111 ... 16.825 21.5 36.0 1.611111 \n", "79 29.0 5.827586 ... 17.300 24.8 29.0 1.275862 \n", "80 29.0 4.137931 ... 18.700 23.7 29.0 2.206897 \n", "81 29.0 4.620690 ... 17.300 20.7 29.0 1.965517 \n", "82 31.0 4.193548 ... 18.000 24.6 31.0 1.645161 \n", "\n", " \n", " std min 25% 50% 75% max \n", "model_year \n", "70 0.603765 1.0 1.0 1.0 1.0 3.0 \n", "71 0.741798 1.0 1.0 1.0 2.0 3.0 \n", "72 0.792658 1.0 1.0 1.0 2.0 3.0 \n", "73 0.667467 1.0 1.0 1.0 2.0 3.0 \n", "74 0.832050 1.0 1.0 1.0 2.0 3.0 \n", "75 0.730297 1.0 1.0 1.0 2.0 3.0 \n", "76 0.706476 1.0 1.0 1.0 2.0 3.0 \n", "77 0.835711 1.0 1.0 1.0 2.0 3.0 \n", "78 0.837608 1.0 1.0 1.0 2.0 3.0 \n", "79 0.591400 1.0 1.0 1.0 1.0 3.0 \n", "80 0.818505 1.0 2.0 2.0 3.0 3.0 \n", "81 0.944259 1.0 1.0 2.0 3.0 3.0 \n", "82 0.914636 1.0 1.0 1.0 3.0 3.0 \n", "\n", "[13 rows x 48 columns]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.groupby('model_year').describe()" ] }, { "cell_type": "code", "execution_count": 12, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
model_year70717273747576777879808182
mpgcount29.00000028.00000028.00000040.00000027.00000030.00000034.00000028.00000036.00000029.00000029.00000029.00000031.000000
mean17.68965521.25000018.71428617.10000022.70370420.26666721.57352923.37500024.06111125.09310333.69655230.33448331.709677
std5.3392316.5919425.4355294.7002456.4200104.9405665.8892976.6758626.8980446.7942177.0379835.5914655.392548
min9.00000012.00000011.00000011.00000013.00000013.00000013.00000015.00000016.20000015.50000019.10000017.60000022.000000
25%14.00000015.50000013.75000013.00000016.00000016.00000016.75000017.37500019.35000019.20000029.80000026.60000027.000000
50%16.00000019.00000018.50000016.00000024.00000019.50000021.00000021.75000020.70000023.90000032.70000031.60000032.000000
75%22.00000027.00000023.00000020.00000027.00000023.00000026.37500030.00000028.00000031.80000038.10000034.40000036.000000
max27.00000035.00000028.00000029.00000032.00000033.00000033.00000036.00000043.10000037.30000046.60000039.10000044.000000
cylinderscount29.00000028.00000028.00000040.00000027.00000030.00000034.00000028.00000036.00000029.00000029.00000029.00000031.000000
mean6.7586215.5714295.8214296.3750005.2592595.6000005.6470595.4642865.3611115.8275864.1379314.6206904.193548
std1.7249261.6650792.0737081.8072151.5833901.5222491.6675581.8152061.4957611.7741990.5808951.0827810.601074
min4.0000004.0000003.0000003.0000004.0000004.0000004.0000003.0000004.0000004.0000003.0000004.0000004.000000
25%6.0000004.0000004.0000004.0000004.0000004.0000004.0000004.0000004.0000004.0000004.0000004.0000004.000000
50%8.0000006.0000004.0000007.0000004.0000006.0000006.0000004.0000005.5000006.0000004.0000004.0000004.000000
75%8.0000006.5000008.0000008.0000006.0000006.0000007.5000008.0000006.0000008.0000004.0000006.0000004.000000
max8.0000008.0000008.0000008.0000008.0000008.0000008.0000008.0000008.0000008.0000006.0000008.0000006.000000
displacementcount29.00000028.00000028.00000040.00000027.00000030.00000034.00000028.00000036.00000029.00000029.00000029.00000031.000000
mean281.413793209.750000218.375000256.875000171.740741205.533333197.794118191.392857177.805556206.689655115.827586135.310345128.870968
std124.421380115.102410123.781964121.72208592.60112787.66973094.422256107.81374276.01271396.30758133.74491458.38792939.352037
min97.00000071.00000070.00000068.00000071.00000090.00000085.00000079.00000078.00000085.00000070.00000079.00000091.000000
25%198.00000097.750000109.250000121.75000090.000000121.000000102.50000097.750000115.500000121.00000090.00000098.000000105.000000
50%307.000000228.500000131.000000276.000000122.000000228.000000184.000000143.000000159.500000183.000000107.000000119.000000119.000000
75%383.000000273.000000326.000000350.250000250.000000250.000000291.000000270.500000231.000000302.000000140.000000151.000000142.000000
max455.000000400.000000429.000000455.000000350.000000400.000000351.000000400.000000318.000000360.000000225.000000350.000000262.000000
weightcount29.00000028.00000028.00000040.00000027.00000030.00000034.00000028.00000036.00000029.00000029.00000029.00000031.000000
mean3372.7931032995.4285713237.7142863419.0250002877.9259263176.8000003078.7352942997.3571432861.8055563055.3448282436.6551722522.9310342453.548387
std852.8686631061.830859974.520960974.809133949.308571765.179781821.371481912.825902626.023907747.881497432.235491533.600501354.276713
min1835.0000001613.0000002100.0000001867.0000001649.0000001795.0000001795.0000001825.0000001800.0000001915.0000001835.0000001755.0000001965.000000
25%2648.0000002110.7500002285.5000002554.5000002116.5000002676.7500002228.7500002135.0000002282.5000002556.0000002110.0000002065.0000002127.500000
50%3449.0000002798.0000002956.0000003338.5000002489.0000003098.5000003171.5000002747.5000002910.0000003190.0000002335.0000002385.0000002525.000000
75%4312.0000003603.2500004169.7500004247.2500003622.5000003662.2500003803.7500003925.0000003410.0000003725.0000002800.0000002900.0000002727.500000
max4732.0000005140.0000004633.0000004997.0000004699.0000004668.0000004380.0000004335.0000004080.0000004360.0000003381.0000003725.0000003035.000000
accelerationcount29.00000028.00000028.00000040.00000027.00000030.00000034.00000028.00000036.00000029.00000029.00000029.00000031.000000
mean12.94827615.14285715.12500014.31250016.20370416.05000015.94117615.43571415.80555615.81379316.93448316.30689716.638710
std3.3309822.6661712.8500322.7542221.6885322.4717372.8014192.2733912.1299152.9529312.8266942.1925092.484844
min8.00000011.50000011.0000009.50000013.50000011.50000012.00000011.10000011.20000011.30000011.40000012.60000011.600000
25%10.00000013.37500013.37500012.50000015.25000014.12500013.92500014.00000014.47500014.00000015.10000014.80000014.850000
50%12.50000014.50000014.50000014.00000016.00000016.00000015.50000015.65000015.75000015.00000016.50000016.20000016.400000
75%15.00000016.12500016.62500016.00000017.00000017.37500017.55000016.92500016.82500017.30000018.70000017.30000018.000000
max20.50000020.50000023.50000021.00000021.00000021.00000022.20000019.00000021.50000024.80000023.70000020.70000024.600000
origincount29.00000028.00000028.00000040.00000027.00000030.00000034.00000028.00000036.00000029.00000029.00000029.00000031.000000
mean1.3103451.4285711.5357141.3750001.6666671.4666671.4705881.5714291.6111111.2758622.2068971.9655171.645161
std0.6037650.7417980.7926580.6674670.8320500.7302970.7064760.8357110.8376080.5914000.8185050.9442590.914636
min1.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.000000
25%1.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000002.0000001.0000001.000000
50%1.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000002.0000002.0000001.000000
75%1.0000002.0000002.0000002.0000002.0000002.0000002.0000002.0000002.0000001.0000003.0000003.0000003.000000
max3.0000003.0000003.0000003.0000003.0000003.0000003.0000003.0000003.0000003.0000003.0000003.0000003.000000
\n", "
" ], "text/plain": [ "model_year 70 71 72 73 \\\n", "mpg count 29.000000 28.000000 28.000000 40.000000 \n", " mean 17.689655 21.250000 18.714286 17.100000 \n", " std 5.339231 6.591942 5.435529 4.700245 \n", " min 9.000000 12.000000 11.000000 11.000000 \n", " 25% 14.000000 15.500000 13.750000 13.000000 \n", " 50% 16.000000 19.000000 18.500000 16.000000 \n", " 75% 22.000000 27.000000 23.000000 20.000000 \n", " max 27.000000 35.000000 28.000000 29.000000 \n", "cylinders count 29.000000 28.000000 28.000000 40.000000 \n", " mean 6.758621 5.571429 5.821429 6.375000 \n", " std 1.724926 1.665079 2.073708 1.807215 \n", " min 4.000000 4.000000 3.000000 3.000000 \n", " 25% 6.000000 4.000000 4.000000 4.000000 \n", " 50% 8.000000 6.000000 4.000000 7.000000 \n", " 75% 8.000000 6.500000 8.000000 8.000000 \n", " max 8.000000 8.000000 8.000000 8.000000 \n", "displacement count 29.000000 28.000000 28.000000 40.000000 \n", " mean 281.413793 209.750000 218.375000 256.875000 \n", " std 124.421380 115.102410 123.781964 121.722085 \n", " min 97.000000 71.000000 70.000000 68.000000 \n", " 25% 198.000000 97.750000 109.250000 121.750000 \n", " 50% 307.000000 228.500000 131.000000 276.000000 \n", " 75% 383.000000 273.000000 326.000000 350.250000 \n", " max 455.000000 400.000000 429.000000 455.000000 \n", "weight count 29.000000 28.000000 28.000000 40.000000 \n", " mean 3372.793103 2995.428571 3237.714286 3419.025000 \n", " std 852.868663 1061.830859 974.520960 974.809133 \n", " min 1835.000000 1613.000000 2100.000000 1867.000000 \n", " 25% 2648.000000 2110.750000 2285.500000 2554.500000 \n", " 50% 3449.000000 2798.000000 2956.000000 3338.500000 \n", " 75% 4312.000000 3603.250000 4169.750000 4247.250000 \n", " max 4732.000000 5140.000000 4633.000000 4997.000000 \n", "acceleration count 29.000000 28.000000 28.000000 40.000000 \n", " mean 12.948276 15.142857 15.125000 14.312500 \n", " std 3.330982 2.666171 2.850032 2.754222 \n", " min 8.000000 11.500000 11.000000 9.500000 \n", " 25% 10.000000 13.375000 13.375000 12.500000 \n", " 50% 12.500000 14.500000 14.500000 14.000000 \n", " 75% 15.000000 16.125000 16.625000 16.000000 \n", " max 20.500000 20.500000 23.500000 21.000000 \n", "origin count 29.000000 28.000000 28.000000 40.000000 \n", " mean 1.310345 1.428571 1.535714 1.375000 \n", " std 0.603765 0.741798 0.792658 0.667467 \n", " min 1.000000 1.000000 1.000000 1.000000 \n", " 25% 1.000000 1.000000 1.000000 1.000000 \n", " 50% 1.000000 1.000000 1.000000 1.000000 \n", " 75% 1.000000 2.000000 2.000000 2.000000 \n", " max 3.000000 3.000000 3.000000 3.000000 \n", "\n", "model_year 74 75 76 77 \\\n", "mpg count 27.000000 30.000000 34.000000 28.000000 \n", " mean 22.703704 20.266667 21.573529 23.375000 \n", " std 6.420010 4.940566 5.889297 6.675862 \n", " min 13.000000 13.000000 13.000000 15.000000 \n", " 25% 16.000000 16.000000 16.750000 17.375000 \n", " 50% 24.000000 19.500000 21.000000 21.750000 \n", " 75% 27.000000 23.000000 26.375000 30.000000 \n", " max 32.000000 33.000000 33.000000 36.000000 \n", "cylinders count 27.000000 30.000000 34.000000 28.000000 \n", " mean 5.259259 5.600000 5.647059 5.464286 \n", " std 1.583390 1.522249 1.667558 1.815206 \n", " min 4.000000 4.000000 4.000000 3.000000 \n", " 25% 4.000000 4.000000 4.000000 4.000000 \n", " 50% 4.000000 6.000000 6.000000 4.000000 \n", " 75% 6.000000 6.000000 7.500000 8.000000 \n", " max 8.000000 8.000000 8.000000 8.000000 \n", "displacement count 27.000000 30.000000 34.000000 28.000000 \n", " mean 171.740741 205.533333 197.794118 191.392857 \n", " std 92.601127 87.669730 94.422256 107.813742 \n", " min 71.000000 90.000000 85.000000 79.000000 \n", " 25% 90.000000 121.000000 102.500000 97.750000 \n", " 50% 122.000000 228.000000 184.000000 143.000000 \n", " 75% 250.000000 250.000000 291.000000 270.500000 \n", " max 350.000000 400.000000 351.000000 400.000000 \n", "weight count 27.000000 30.000000 34.000000 28.000000 \n", " mean 2877.925926 3176.800000 3078.735294 2997.357143 \n", " std 949.308571 765.179781 821.371481 912.825902 \n", " min 1649.000000 1795.000000 1795.000000 1825.000000 \n", " 25% 2116.500000 2676.750000 2228.750000 2135.000000 \n", " 50% 2489.000000 3098.500000 3171.500000 2747.500000 \n", " 75% 3622.500000 3662.250000 3803.750000 3925.000000 \n", " max 4699.000000 4668.000000 4380.000000 4335.000000 \n", "acceleration count 27.000000 30.000000 34.000000 28.000000 \n", " mean 16.203704 16.050000 15.941176 15.435714 \n", " std 1.688532 2.471737 2.801419 2.273391 \n", " min 13.500000 11.500000 12.000000 11.100000 \n", " 25% 15.250000 14.125000 13.925000 14.000000 \n", " 50% 16.000000 16.000000 15.500000 15.650000 \n", " 75% 17.000000 17.375000 17.550000 16.925000 \n", " max 21.000000 21.000000 22.200000 19.000000 \n", "origin count 27.000000 30.000000 34.000000 28.000000 \n", " mean 1.666667 1.466667 1.470588 1.571429 \n", " std 0.832050 0.730297 0.706476 0.835711 \n", " min 1.000000 1.000000 1.000000 1.000000 \n", " 25% 1.000000 1.000000 1.000000 1.000000 \n", " 50% 1.000000 1.000000 1.000000 1.000000 \n", " 75% 2.000000 2.000000 2.000000 2.000000 \n", " max 3.000000 3.000000 3.000000 3.000000 \n", "\n", "model_year 78 79 80 81 \\\n", "mpg count 36.000000 29.000000 29.000000 29.000000 \n", " mean 24.061111 25.093103 33.696552 30.334483 \n", " std 6.898044 6.794217 7.037983 5.591465 \n", " min 16.200000 15.500000 19.100000 17.600000 \n", " 25% 19.350000 19.200000 29.800000 26.600000 \n", " 50% 20.700000 23.900000 32.700000 31.600000 \n", " 75% 28.000000 31.800000 38.100000 34.400000 \n", " max 43.100000 37.300000 46.600000 39.100000 \n", "cylinders count 36.000000 29.000000 29.000000 29.000000 \n", " mean 5.361111 5.827586 4.137931 4.620690 \n", " std 1.495761 1.774199 0.580895 1.082781 \n", " min 4.000000 4.000000 3.000000 4.000000 \n", " 25% 4.000000 4.000000 4.000000 4.000000 \n", " 50% 5.500000 6.000000 4.000000 4.000000 \n", " 75% 6.000000 8.000000 4.000000 6.000000 \n", " max 8.000000 8.000000 6.000000 8.000000 \n", "displacement count 36.000000 29.000000 29.000000 29.000000 \n", " mean 177.805556 206.689655 115.827586 135.310345 \n", " std 76.012713 96.307581 33.744914 58.387929 \n", " min 78.000000 85.000000 70.000000 79.000000 \n", " 25% 115.500000 121.000000 90.000000 98.000000 \n", " 50% 159.500000 183.000000 107.000000 119.000000 \n", " 75% 231.000000 302.000000 140.000000 151.000000 \n", " max 318.000000 360.000000 225.000000 350.000000 \n", "weight count 36.000000 29.000000 29.000000 29.000000 \n", " mean 2861.805556 3055.344828 2436.655172 2522.931034 \n", " std 626.023907 747.881497 432.235491 533.600501 \n", " min 1800.000000 1915.000000 1835.000000 1755.000000 \n", " 25% 2282.500000 2556.000000 2110.000000 2065.000000 \n", " 50% 2910.000000 3190.000000 2335.000000 2385.000000 \n", " 75% 3410.000000 3725.000000 2800.000000 2900.000000 \n", " max 4080.000000 4360.000000 3381.000000 3725.000000 \n", "acceleration count 36.000000 29.000000 29.000000 29.000000 \n", " mean 15.805556 15.813793 16.934483 16.306897 \n", " std 2.129915 2.952931 2.826694 2.192509 \n", " min 11.200000 11.300000 11.400000 12.600000 \n", " 25% 14.475000 14.000000 15.100000 14.800000 \n", " 50% 15.750000 15.000000 16.500000 16.200000 \n", " 75% 16.825000 17.300000 18.700000 17.300000 \n", " max 21.500000 24.800000 23.700000 20.700000 \n", "origin count 36.000000 29.000000 29.000000 29.000000 \n", " mean 1.611111 1.275862 2.206897 1.965517 \n", " std 0.837608 0.591400 0.818505 0.944259 \n", " min 1.000000 1.000000 1.000000 1.000000 \n", " 25% 1.000000 1.000000 2.000000 1.000000 \n", " 50% 1.000000 1.000000 2.000000 2.000000 \n", " 75% 2.000000 1.000000 3.000000 3.000000 \n", " max 3.000000 3.000000 3.000000 3.000000 \n", "\n", "model_year 82 \n", "mpg count 31.000000 \n", " mean 31.709677 \n", " std 5.392548 \n", " min 22.000000 \n", " 25% 27.000000 \n", " 50% 32.000000 \n", " 75% 36.000000 \n", " max 44.000000 \n", "cylinders count 31.000000 \n", " mean 4.193548 \n", " std 0.601074 \n", " min 4.000000 \n", " 25% 4.000000 \n", " 50% 4.000000 \n", " 75% 4.000000 \n", " max 6.000000 \n", "displacement count 31.000000 \n", " mean 128.870968 \n", " std 39.352037 \n", " min 91.000000 \n", " 25% 105.000000 \n", " 50% 119.000000 \n", " 75% 142.000000 \n", " max 262.000000 \n", "weight count 31.000000 \n", " mean 2453.548387 \n", " std 354.276713 \n", " min 1965.000000 \n", " 25% 2127.500000 \n", " 50% 2525.000000 \n", " 75% 2727.500000 \n", " max 3035.000000 \n", "acceleration count 31.000000 \n", " mean 16.638710 \n", " std 2.484844 \n", " min 11.600000 \n", " 25% 14.850000 \n", " 50% 16.400000 \n", " 75% 18.000000 \n", " max 24.600000 \n", "origin count 31.000000 \n", " mean 1.645161 \n", " std 0.914636 \n", " min 1.000000 \n", " 25% 1.000000 \n", " 50% 1.000000 \n", " 75% 3.000000 \n", " max 3.000000 " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.groupby('model_year').describe().transpose()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Groupby Multiple Columns\n", "Let's explore average mpg per year per cylinder count" ] }, { "cell_type": "code", "execution_count": 13, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mpgdisplacementweightaccelerationorigin
model_yearcylinders
70425.285714107.0000002292.57142916.0000002.285714
620.500000199.0000002710.50000015.5000001.000000
814.111111367.5555563940.05555611.1944441.000000
71427.461538101.8461542056.38461516.9615381.923077
618.000000243.3750003171.87500014.7500001.000000
813.428571371.7142864537.71428612.2142861.000000
72319.00000070.0000002330.00000013.5000003.000000
423.428571111.5357142382.64285717.2142861.928571
813.615385344.8461544228.38461513.0000001.000000
73318.00000070.0000002124.00000013.5000003.000000
422.727273109.2727272338.09090917.1363642.000000
619.000000212.2500002917.12500015.6875001.250000
813.200000365.2500004279.05000012.2500001.000000
74427.80000096.5333332151.46666716.4000002.200000
617.857143230.4285713320.00000016.8571431.000000
814.200000315.2000004438.40000014.7000001.000000
75425.250000114.8333332489.25000015.8333332.166667
617.583333233.7500003398.33333317.7083331.000000
815.666667330.5000004108.83333313.1666671.000000
76426.766667106.3333332306.60000016.8666671.866667
620.000000221.4000003349.60000017.0000001.300000
814.666667324.0000004064.66666713.2222221.000000
77321.50000080.0000002720.00000013.5000003.000000
429.107143106.5000002205.07142916.0642861.857143
619.500000220.4000003383.00000016.9000001.400000
816.000000335.7500004177.50000013.6625001.000000
78429.576471112.1176472296.76470616.2823532.117647
520.300000131.0000002830.00000015.9000002.000000
619.066667213.2500003314.16666716.3916671.166667
819.050000300.8333333563.33333313.2666671.000000
79431.525000113.5833332357.58333315.9916671.583333
525.400000183.0000003530.00000020.1000002.000000
622.950000205.6666673025.83333315.4333331.000000
818.630000321.4000003862.90000015.4000001.000000
80323.70000070.0000002420.00000012.5000003.000000
434.612000111.0000002360.08000017.1440002.200000
536.400000121.0000002950.00000019.9000002.000000
625.900000196.5000003145.50000015.0500002.000000
81432.814286108.8571432275.47619016.4666672.095238
623.428571184.0000003093.57142915.4428571.714286
826.600000350.0000003725.00000019.0000001.000000
82432.071429118.5714292402.32142916.7035711.714286
628.333333225.0000002931.66666716.0333331.000000
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration \\\n", "model_year cylinders \n", "70 4 25.285714 107.000000 2292.571429 16.000000 \n", " 6 20.500000 199.000000 2710.500000 15.500000 \n", " 8 14.111111 367.555556 3940.055556 11.194444 \n", "71 4 27.461538 101.846154 2056.384615 16.961538 \n", " 6 18.000000 243.375000 3171.875000 14.750000 \n", " 8 13.428571 371.714286 4537.714286 12.214286 \n", "72 3 19.000000 70.000000 2330.000000 13.500000 \n", " 4 23.428571 111.535714 2382.642857 17.214286 \n", " 8 13.615385 344.846154 4228.384615 13.000000 \n", "73 3 18.000000 70.000000 2124.000000 13.500000 \n", " 4 22.727273 109.272727 2338.090909 17.136364 \n", " 6 19.000000 212.250000 2917.125000 15.687500 \n", " 8 13.200000 365.250000 4279.050000 12.250000 \n", "74 4 27.800000 96.533333 2151.466667 16.400000 \n", " 6 17.857143 230.428571 3320.000000 16.857143 \n", " 8 14.200000 315.200000 4438.400000 14.700000 \n", "75 4 25.250000 114.833333 2489.250000 15.833333 \n", " 6 17.583333 233.750000 3398.333333 17.708333 \n", " 8 15.666667 330.500000 4108.833333 13.166667 \n", "76 4 26.766667 106.333333 2306.600000 16.866667 \n", " 6 20.000000 221.400000 3349.600000 17.000000 \n", " 8 14.666667 324.000000 4064.666667 13.222222 \n", "77 3 21.500000 80.000000 2720.000000 13.500000 \n", " 4 29.107143 106.500000 2205.071429 16.064286 \n", " 6 19.500000 220.400000 3383.000000 16.900000 \n", " 8 16.000000 335.750000 4177.500000 13.662500 \n", "78 4 29.576471 112.117647 2296.764706 16.282353 \n", " 5 20.300000 131.000000 2830.000000 15.900000 \n", " 6 19.066667 213.250000 3314.166667 16.391667 \n", " 8 19.050000 300.833333 3563.333333 13.266667 \n", "79 4 31.525000 113.583333 2357.583333 15.991667 \n", " 5 25.400000 183.000000 3530.000000 20.100000 \n", " 6 22.950000 205.666667 3025.833333 15.433333 \n", " 8 18.630000 321.400000 3862.900000 15.400000 \n", "80 3 23.700000 70.000000 2420.000000 12.500000 \n", " 4 34.612000 111.000000 2360.080000 17.144000 \n", " 5 36.400000 121.000000 2950.000000 19.900000 \n", " 6 25.900000 196.500000 3145.500000 15.050000 \n", "81 4 32.814286 108.857143 2275.476190 16.466667 \n", " 6 23.428571 184.000000 3093.571429 15.442857 \n", " 8 26.600000 350.000000 3725.000000 19.000000 \n", "82 4 32.071429 118.571429 2402.321429 16.703571 \n", " 6 28.333333 225.000000 2931.666667 16.033333 \n", "\n", " origin \n", "model_year cylinders \n", "70 4 2.285714 \n", " 6 1.000000 \n", " 8 1.000000 \n", "71 4 1.923077 \n", " 6 1.000000 \n", " 8 1.000000 \n", "72 3 3.000000 \n", " 4 1.928571 \n", " 8 1.000000 \n", "73 3 3.000000 \n", " 4 2.000000 \n", " 6 1.250000 \n", " 8 1.000000 \n", "74 4 2.200000 \n", " 6 1.000000 \n", " 8 1.000000 \n", "75 4 2.166667 \n", " 6 1.000000 \n", " 8 1.000000 \n", "76 4 1.866667 \n", " 6 1.300000 \n", " 8 1.000000 \n", "77 3 3.000000 \n", " 4 1.857143 \n", " 6 1.400000 \n", " 8 1.000000 \n", "78 4 2.117647 \n", " 5 2.000000 \n", " 6 1.166667 \n", " 8 1.000000 \n", "79 4 1.583333 \n", " 5 2.000000 \n", " 6 1.000000 \n", " 8 1.000000 \n", "80 3 3.000000 \n", " 4 2.200000 \n", " 5 2.000000 \n", " 6 2.000000 \n", "81 4 2.095238 \n", " 6 1.714286 \n", " 8 1.000000 \n", "82 4 1.714286 \n", " 6 1.000000 " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.groupby(['model_year','cylinders']).mean()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MultiIndex([(70, 4),\n", " (70, 6),\n", " (70, 8),\n", " (71, 4),\n", " (71, 6),\n", " (71, 8),\n", " (72, 3),\n", " (72, 4),\n", " (72, 8),\n", " (73, 3),\n", " (73, 4),\n", " (73, 6),\n", " (73, 8),\n", " (74, 4),\n", " (74, 6),\n", " (74, 8),\n", " (75, 4),\n", " (75, 6),\n", " (75, 8),\n", " (76, 4),\n", " (76, 6),\n", " (76, 8),\n", " (77, 3),\n", " (77, 4),\n", " (77, 6),\n", " (77, 8),\n", " (78, 4),\n", " (78, 5),\n", " (78, 6),\n", " (78, 8),\n", " (79, 4),\n", " (79, 5),\n", " (79, 6),\n", " (79, 8),\n", " (80, 3),\n", " (80, 4),\n", " (80, 5),\n", " (80, 6),\n", " (81, 4),\n", " (81, 6),\n", " (81, 8),\n", " (82, 4),\n", " (82, 6)],\n", " names=['model_year', 'cylinders'])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.groupby(['model_year','cylinders']).mean().index" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# MultiIndex\n", "\n", "## The MultiIndex Object" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "year_cyl = df.groupby(['model_year','cylinders']).mean()" ] }, { "cell_type": "code", "execution_count": 16, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mpgdisplacementweightaccelerationorigin
model_yearcylinders
70425.285714107.0000002292.57142916.0000002.285714
620.500000199.0000002710.50000015.5000001.000000
814.111111367.5555563940.05555611.1944441.000000
71427.461538101.8461542056.38461516.9615381.923077
618.000000243.3750003171.87500014.7500001.000000
813.428571371.7142864537.71428612.2142861.000000
72319.00000070.0000002330.00000013.5000003.000000
423.428571111.5357142382.64285717.2142861.928571
813.615385344.8461544228.38461513.0000001.000000
73318.00000070.0000002124.00000013.5000003.000000
422.727273109.2727272338.09090917.1363642.000000
619.000000212.2500002917.12500015.6875001.250000
813.200000365.2500004279.05000012.2500001.000000
74427.80000096.5333332151.46666716.4000002.200000
617.857143230.4285713320.00000016.8571431.000000
814.200000315.2000004438.40000014.7000001.000000
75425.250000114.8333332489.25000015.8333332.166667
617.583333233.7500003398.33333317.7083331.000000
815.666667330.5000004108.83333313.1666671.000000
76426.766667106.3333332306.60000016.8666671.866667
620.000000221.4000003349.60000017.0000001.300000
814.666667324.0000004064.66666713.2222221.000000
77321.50000080.0000002720.00000013.5000003.000000
429.107143106.5000002205.07142916.0642861.857143
619.500000220.4000003383.00000016.9000001.400000
816.000000335.7500004177.50000013.6625001.000000
78429.576471112.1176472296.76470616.2823532.117647
520.300000131.0000002830.00000015.9000002.000000
619.066667213.2500003314.16666716.3916671.166667
819.050000300.8333333563.33333313.2666671.000000
79431.525000113.5833332357.58333315.9916671.583333
525.400000183.0000003530.00000020.1000002.000000
622.950000205.6666673025.83333315.4333331.000000
818.630000321.4000003862.90000015.4000001.000000
80323.70000070.0000002420.00000012.5000003.000000
434.612000111.0000002360.08000017.1440002.200000
536.400000121.0000002950.00000019.9000002.000000
625.900000196.5000003145.50000015.0500002.000000
81432.814286108.8571432275.47619016.4666672.095238
623.428571184.0000003093.57142915.4428571.714286
826.600000350.0000003725.00000019.0000001.000000
82432.071429118.5714292402.32142916.7035711.714286
628.333333225.0000002931.66666716.0333331.000000
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration \\\n", "model_year cylinders \n", "70 4 25.285714 107.000000 2292.571429 16.000000 \n", " 6 20.500000 199.000000 2710.500000 15.500000 \n", " 8 14.111111 367.555556 3940.055556 11.194444 \n", "71 4 27.461538 101.846154 2056.384615 16.961538 \n", " 6 18.000000 243.375000 3171.875000 14.750000 \n", " 8 13.428571 371.714286 4537.714286 12.214286 \n", "72 3 19.000000 70.000000 2330.000000 13.500000 \n", " 4 23.428571 111.535714 2382.642857 17.214286 \n", " 8 13.615385 344.846154 4228.384615 13.000000 \n", "73 3 18.000000 70.000000 2124.000000 13.500000 \n", " 4 22.727273 109.272727 2338.090909 17.136364 \n", " 6 19.000000 212.250000 2917.125000 15.687500 \n", " 8 13.200000 365.250000 4279.050000 12.250000 \n", "74 4 27.800000 96.533333 2151.466667 16.400000 \n", " 6 17.857143 230.428571 3320.000000 16.857143 \n", " 8 14.200000 315.200000 4438.400000 14.700000 \n", "75 4 25.250000 114.833333 2489.250000 15.833333 \n", " 6 17.583333 233.750000 3398.333333 17.708333 \n", " 8 15.666667 330.500000 4108.833333 13.166667 \n", "76 4 26.766667 106.333333 2306.600000 16.866667 \n", " 6 20.000000 221.400000 3349.600000 17.000000 \n", " 8 14.666667 324.000000 4064.666667 13.222222 \n", "77 3 21.500000 80.000000 2720.000000 13.500000 \n", " 4 29.107143 106.500000 2205.071429 16.064286 \n", " 6 19.500000 220.400000 3383.000000 16.900000 \n", " 8 16.000000 335.750000 4177.500000 13.662500 \n", "78 4 29.576471 112.117647 2296.764706 16.282353 \n", " 5 20.300000 131.000000 2830.000000 15.900000 \n", " 6 19.066667 213.250000 3314.166667 16.391667 \n", " 8 19.050000 300.833333 3563.333333 13.266667 \n", "79 4 31.525000 113.583333 2357.583333 15.991667 \n", " 5 25.400000 183.000000 3530.000000 20.100000 \n", " 6 22.950000 205.666667 3025.833333 15.433333 \n", " 8 18.630000 321.400000 3862.900000 15.400000 \n", "80 3 23.700000 70.000000 2420.000000 12.500000 \n", " 4 34.612000 111.000000 2360.080000 17.144000 \n", " 5 36.400000 121.000000 2950.000000 19.900000 \n", " 6 25.900000 196.500000 3145.500000 15.050000 \n", "81 4 32.814286 108.857143 2275.476190 16.466667 \n", " 6 23.428571 184.000000 3093.571429 15.442857 \n", " 8 26.600000 350.000000 3725.000000 19.000000 \n", "82 4 32.071429 118.571429 2402.321429 16.703571 \n", " 6 28.333333 225.000000 2931.666667 16.033333 \n", "\n", " origin \n", "model_year cylinders \n", "70 4 2.285714 \n", " 6 1.000000 \n", " 8 1.000000 \n", "71 4 1.923077 \n", " 6 1.000000 \n", " 8 1.000000 \n", "72 3 3.000000 \n", " 4 1.928571 \n", " 8 1.000000 \n", "73 3 3.000000 \n", " 4 2.000000 \n", " 6 1.250000 \n", " 8 1.000000 \n", "74 4 2.200000 \n", " 6 1.000000 \n", " 8 1.000000 \n", "75 4 2.166667 \n", " 6 1.000000 \n", " 8 1.000000 \n", "76 4 1.866667 \n", " 6 1.300000 \n", " 8 1.000000 \n", "77 3 3.000000 \n", " 4 1.857143 \n", " 6 1.400000 \n", " 8 1.000000 \n", "78 4 2.117647 \n", " 5 2.000000 \n", " 6 1.166667 \n", " 8 1.000000 \n", "79 4 1.583333 \n", " 5 2.000000 \n", " 6 1.000000 \n", " 8 1.000000 \n", "80 3 3.000000 \n", " 4 2.200000 \n", " 5 2.000000 \n", " 6 2.000000 \n", "81 4 2.095238 \n", " 6 1.714286 \n", " 8 1.000000 \n", "82 4 1.714286 \n", " 6 1.000000 " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "MultiIndex([(70, 4),\n", " (70, 6),\n", " (70, 8),\n", " (71, 4),\n", " (71, 6),\n", " (71, 8),\n", " (72, 3),\n", " (72, 4),\n", " (72, 8),\n", " (73, 3),\n", " (73, 4),\n", " (73, 6),\n", " (73, 8),\n", " (74, 4),\n", " (74, 6),\n", " (74, 8),\n", " (75, 4),\n", " (75, 6),\n", " (75, 8),\n", " (76, 4),\n", " (76, 6),\n", " (76, 8),\n", " (77, 3),\n", " (77, 4),\n", " (77, 6),\n", " (77, 8),\n", " (78, 4),\n", " (78, 5),\n", " (78, 6),\n", " (78, 8),\n", " (79, 4),\n", " (79, 5),\n", " (79, 6),\n", " (79, 8),\n", " (80, 3),\n", " (80, 4),\n", " (80, 5),\n", " (80, 6),\n", " (81, 4),\n", " (81, 6),\n", " (81, 8),\n", " (82, 4),\n", " (82, 6)],\n", " names=['model_year', 'cylinders'])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.index" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "FrozenList([[70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82], [3, 4, 5, 6, 8]])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.index.levels" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "FrozenList(['model_year', 'cylinders'])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.index.names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Indexing with the Hierarchical Index\n", "\n", "Full Documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html" ] }, { "cell_type": "code", "execution_count": 20, "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", "
mpgdisplacementweightaccelerationorigin
model_yearcylinders
70425.285714107.0000002292.57142916.0000002.285714
620.500000199.0000002710.50000015.5000001.000000
814.111111367.5555563940.05555611.1944441.000000
71427.461538101.8461542056.38461516.9615381.923077
618.000000243.3750003171.87500014.7500001.000000
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration \\\n", "model_year cylinders \n", "70 4 25.285714 107.000000 2292.571429 16.000000 \n", " 6 20.500000 199.000000 2710.500000 15.500000 \n", " 8 14.111111 367.555556 3940.055556 11.194444 \n", "71 4 27.461538 101.846154 2056.384615 16.961538 \n", " 6 18.000000 243.375000 3171.875000 14.750000 \n", "\n", " origin \n", "model_year cylinders \n", "70 4 2.285714 \n", " 6 1.000000 \n", " 8 1.000000 \n", "71 4 1.923077 \n", " 6 1.000000 " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Grab Based on Outside Index" ] }, { "cell_type": "code", "execution_count": 21, "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", "
mpgdisplacementweightaccelerationorigin
cylinders
425.285714107.0000002292.57142916.0000002.285714
620.500000199.0000002710.50000015.5000001.000000
814.111111367.5555563940.05555611.1944441.000000
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration origin\n", "cylinders \n", "4 25.285714 107.000000 2292.571429 16.000000 2.285714\n", "6 20.500000 199.000000 2710.500000 15.500000 1.000000\n", "8 14.111111 367.555556 3940.055556 11.194444 1.000000" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.loc[70]" ] }, { "cell_type": "code", "execution_count": 22, "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", "
mpgdisplacementweightaccelerationorigin
model_yearcylinders
70425.285714107.0000002292.57142916.0000002.285714
620.500000199.0000002710.50000015.5000001.000000
814.111111367.5555563940.05555611.1944441.000000
72319.00000070.0000002330.00000013.5000003.000000
423.428571111.5357142382.64285717.2142861.928571
813.615385344.8461544228.38461513.0000001.000000
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration \\\n", "model_year cylinders \n", "70 4 25.285714 107.000000 2292.571429 16.000000 \n", " 6 20.500000 199.000000 2710.500000 15.500000 \n", " 8 14.111111 367.555556 3940.055556 11.194444 \n", "72 3 19.000000 70.000000 2330.000000 13.500000 \n", " 4 23.428571 111.535714 2382.642857 17.214286 \n", " 8 13.615385 344.846154 4228.384615 13.000000 \n", "\n", " origin \n", "model_year cylinders \n", "70 4 2.285714 \n", " 6 1.000000 \n", " 8 1.000000 \n", "72 3 3.000000 \n", " 4 1.928571 \n", " 8 1.000000 " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.loc[[70,72]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Grab a Single Row" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "mpg 14.111111\n", "displacement 367.555556\n", "weight 3940.055556\n", "acceleration 11.194444\n", "origin 1.000000\n", "Name: (70, 8), dtype: float64" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.loc[(70,8)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Grab Based on Cross-section with .xs()\n", "\n", "This method takes a `key` argument to select data at a particular\n", "level of a MultiIndex.\n", "\n", "Parameters\n", "----------\n", " key : label or tuple of label\n", " Label contained in the index, or partially in a MultiIndex.\n", " axis : {0 or 'index', 1 or 'columns'}, default 0\n", " Axis to retrieve cross-section on.\n", " level : object, defaults to first n levels (n=1 or len(key))\n", " In case of a key partially contained in a MultiIndex, indicate\n", " which levels are used. Levels can be referred by label or position." ] }, { "cell_type": "code", "execution_count": 24, "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", "
mpgdisplacementweightaccelerationorigin
cylinders
425.285714107.0000002292.57142916.0000002.285714
620.500000199.0000002710.50000015.5000001.000000
814.111111367.5555563940.05555611.1944441.000000
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration origin\n", "cylinders \n", "4 25.285714 107.000000 2292.571429 16.000000 2.285714\n", "6 20.500000 199.000000 2710.500000 15.500000 1.000000\n", "8 14.111111 367.555556 3940.055556 11.194444 1.000000" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.xs(key=70,axis=0,level='model_year')" ] }, { "cell_type": "code", "execution_count": 25, "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", "
mpgdisplacementweightaccelerationorigin
model_year
7025.285714107.0000002292.57142916.0000002.285714
7127.461538101.8461542056.38461516.9615381.923077
7223.428571111.5357142382.64285717.2142861.928571
7322.727273109.2727272338.09090917.1363642.000000
7427.80000096.5333332151.46666716.4000002.200000
7525.250000114.8333332489.25000015.8333332.166667
7626.766667106.3333332306.60000016.8666671.866667
7729.107143106.5000002205.07142916.0642861.857143
7829.576471112.1176472296.76470616.2823532.117647
7931.525000113.5833332357.58333315.9916671.583333
8034.612000111.0000002360.08000017.1440002.200000
8132.814286108.8571432275.47619016.4666672.095238
8232.071429118.5714292402.32142916.7035711.714286
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration origin\n", "model_year \n", "70 25.285714 107.000000 2292.571429 16.000000 2.285714\n", "71 27.461538 101.846154 2056.384615 16.961538 1.923077\n", "72 23.428571 111.535714 2382.642857 17.214286 1.928571\n", "73 22.727273 109.272727 2338.090909 17.136364 2.000000\n", "74 27.800000 96.533333 2151.466667 16.400000 2.200000\n", "75 25.250000 114.833333 2489.250000 15.833333 2.166667\n", "76 26.766667 106.333333 2306.600000 16.866667 1.866667\n", "77 29.107143 106.500000 2205.071429 16.064286 1.857143\n", "78 29.576471 112.117647 2296.764706 16.282353 2.117647\n", "79 31.525000 113.583333 2357.583333 15.991667 1.583333\n", "80 34.612000 111.000000 2360.080000 17.144000 2.200000\n", "81 32.814286 108.857143 2275.476190 16.466667 2.095238\n", "82 32.071429 118.571429 2402.321429 16.703571 1.714286" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Mean column values for 4 cylinders per year\n", "year_cyl.xs(key=4,axis=0,level='cylinders')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Careful note!\n", "\n", "Keep in mind, its usually much easier to filter out values **before** running a groupby() call, so you should attempt to filter out any values/categories you don't want to use. For example, its much easier to remove **4** cylinder cars before the groupby() call, very difficult to this sort of thing after a group by." ] }, { "cell_type": "code", "execution_count": 26, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mpgdisplacementweightaccelerationorigin
model_yearcylinders
70620.500000199.0000002710.50000015.5000001.000000
814.111111367.5555563940.05555611.1944441.000000
71618.000000243.3750003171.87500014.7500001.000000
813.428571371.7142864537.71428612.2142861.000000
72813.615385344.8461544228.38461513.0000001.000000
73619.000000212.2500002917.12500015.6875001.250000
813.200000365.2500004279.05000012.2500001.000000
74617.857143230.4285713320.00000016.8571431.000000
814.200000315.2000004438.40000014.7000001.000000
75617.583333233.7500003398.33333317.7083331.000000
815.666667330.5000004108.83333313.1666671.000000
76620.000000221.4000003349.60000017.0000001.300000
814.666667324.0000004064.66666713.2222221.000000
77619.500000220.4000003383.00000016.9000001.400000
816.000000335.7500004177.50000013.6625001.000000
78619.066667213.2500003314.16666716.3916671.166667
819.050000300.8333333563.33333313.2666671.000000
79622.950000205.6666673025.83333315.4333331.000000
818.630000321.4000003862.90000015.4000001.000000
80625.900000196.5000003145.50000015.0500002.000000
81623.428571184.0000003093.57142915.4428571.714286
826.600000350.0000003725.00000019.0000001.000000
82628.333333225.0000002931.66666716.0333331.000000
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration \\\n", "model_year cylinders \n", "70 6 20.500000 199.000000 2710.500000 15.500000 \n", " 8 14.111111 367.555556 3940.055556 11.194444 \n", "71 6 18.000000 243.375000 3171.875000 14.750000 \n", " 8 13.428571 371.714286 4537.714286 12.214286 \n", "72 8 13.615385 344.846154 4228.384615 13.000000 \n", "73 6 19.000000 212.250000 2917.125000 15.687500 \n", " 8 13.200000 365.250000 4279.050000 12.250000 \n", "74 6 17.857143 230.428571 3320.000000 16.857143 \n", " 8 14.200000 315.200000 4438.400000 14.700000 \n", "75 6 17.583333 233.750000 3398.333333 17.708333 \n", " 8 15.666667 330.500000 4108.833333 13.166667 \n", "76 6 20.000000 221.400000 3349.600000 17.000000 \n", " 8 14.666667 324.000000 4064.666667 13.222222 \n", "77 6 19.500000 220.400000 3383.000000 16.900000 \n", " 8 16.000000 335.750000 4177.500000 13.662500 \n", "78 6 19.066667 213.250000 3314.166667 16.391667 \n", " 8 19.050000 300.833333 3563.333333 13.266667 \n", "79 6 22.950000 205.666667 3025.833333 15.433333 \n", " 8 18.630000 321.400000 3862.900000 15.400000 \n", "80 6 25.900000 196.500000 3145.500000 15.050000 \n", "81 6 23.428571 184.000000 3093.571429 15.442857 \n", " 8 26.600000 350.000000 3725.000000 19.000000 \n", "82 6 28.333333 225.000000 2931.666667 16.033333 \n", "\n", " origin \n", "model_year cylinders \n", "70 6 1.000000 \n", " 8 1.000000 \n", "71 6 1.000000 \n", " 8 1.000000 \n", "72 8 1.000000 \n", "73 6 1.250000 \n", " 8 1.000000 \n", "74 6 1.000000 \n", " 8 1.000000 \n", "75 6 1.000000 \n", " 8 1.000000 \n", "76 6 1.300000 \n", " 8 1.000000 \n", "77 6 1.400000 \n", " 8 1.000000 \n", "78 6 1.166667 \n", " 8 1.000000 \n", "79 6 1.000000 \n", " 8 1.000000 \n", "80 6 2.000000 \n", "81 6 1.714286 \n", " 8 1.000000 \n", "82 6 1.000000 " ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[df['cylinders'].isin([6,8])].groupby(['model_year','cylinders']).mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Swap Levels\n", "\n", "* Swapping Levels: https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html#swapping-levels-with-swaplevel\n", "* Generalized Method is reorder_levels: https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html#reordering-levels-with-reorder-levels" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mpgdisplacementweightaccelerationorigin
cylindersmodel_year
47025.285714107.0000002292.57142916.0000002.285714
67020.500000199.0000002710.50000015.5000001.000000
87014.111111367.5555563940.05555611.1944441.000000
47127.461538101.8461542056.38461516.9615381.923077
67118.000000243.3750003171.87500014.7500001.000000
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration \\\n", "cylinders model_year \n", "4 70 25.285714 107.000000 2292.571429 16.000000 \n", "6 70 20.500000 199.000000 2710.500000 15.500000 \n", "8 70 14.111111 367.555556 3940.055556 11.194444 \n", "4 71 27.461538 101.846154 2056.384615 16.961538 \n", "6 71 18.000000 243.375000 3171.875000 14.750000 \n", "\n", " origin \n", "cylinders model_year \n", "4 70 2.285714 \n", "6 70 1.000000 \n", "8 70 1.000000 \n", "4 71 1.923077 \n", "6 71 1.000000 " ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.swaplevel().head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sorting MultiIndex\n", "\n", "* https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html#sorting-a-multiindex " ] }, { "cell_type": "code", "execution_count": 28, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mpgdisplacementweightaccelerationorigin
model_yearcylinders
82628.333333225.0000002931.66666716.0333331.000000
432.071429118.5714292402.32142916.7035711.714286
81826.600000350.0000003725.00000019.0000001.000000
623.428571184.0000003093.57142915.4428571.714286
432.814286108.8571432275.47619016.4666672.095238
80625.900000196.5000003145.50000015.0500002.000000
536.400000121.0000002950.00000019.9000002.000000
434.612000111.0000002360.08000017.1440002.200000
323.70000070.0000002420.00000012.5000003.000000
79818.630000321.4000003862.90000015.4000001.000000
622.950000205.6666673025.83333315.4333331.000000
525.400000183.0000003530.00000020.1000002.000000
431.525000113.5833332357.58333315.9916671.583333
78819.050000300.8333333563.33333313.2666671.000000
619.066667213.2500003314.16666716.3916671.166667
520.300000131.0000002830.00000015.9000002.000000
429.576471112.1176472296.76470616.2823532.117647
77816.000000335.7500004177.50000013.6625001.000000
619.500000220.4000003383.00000016.9000001.400000
429.107143106.5000002205.07142916.0642861.857143
321.50000080.0000002720.00000013.5000003.000000
76814.666667324.0000004064.66666713.2222221.000000
620.000000221.4000003349.60000017.0000001.300000
426.766667106.3333332306.60000016.8666671.866667
75815.666667330.5000004108.83333313.1666671.000000
617.583333233.7500003398.33333317.7083331.000000
425.250000114.8333332489.25000015.8333332.166667
74814.200000315.2000004438.40000014.7000001.000000
617.857143230.4285713320.00000016.8571431.000000
427.80000096.5333332151.46666716.4000002.200000
73813.200000365.2500004279.05000012.2500001.000000
619.000000212.2500002917.12500015.6875001.250000
422.727273109.2727272338.09090917.1363642.000000
318.00000070.0000002124.00000013.5000003.000000
72813.615385344.8461544228.38461513.0000001.000000
423.428571111.5357142382.64285717.2142861.928571
319.00000070.0000002330.00000013.5000003.000000
71813.428571371.7142864537.71428612.2142861.000000
618.000000243.3750003171.87500014.7500001.000000
427.461538101.8461542056.38461516.9615381.923077
70814.111111367.5555563940.05555611.1944441.000000
620.500000199.0000002710.50000015.5000001.000000
425.285714107.0000002292.57142916.0000002.285714
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration \\\n", "model_year cylinders \n", "82 6 28.333333 225.000000 2931.666667 16.033333 \n", " 4 32.071429 118.571429 2402.321429 16.703571 \n", "81 8 26.600000 350.000000 3725.000000 19.000000 \n", " 6 23.428571 184.000000 3093.571429 15.442857 \n", " 4 32.814286 108.857143 2275.476190 16.466667 \n", "80 6 25.900000 196.500000 3145.500000 15.050000 \n", " 5 36.400000 121.000000 2950.000000 19.900000 \n", " 4 34.612000 111.000000 2360.080000 17.144000 \n", " 3 23.700000 70.000000 2420.000000 12.500000 \n", "79 8 18.630000 321.400000 3862.900000 15.400000 \n", " 6 22.950000 205.666667 3025.833333 15.433333 \n", " 5 25.400000 183.000000 3530.000000 20.100000 \n", " 4 31.525000 113.583333 2357.583333 15.991667 \n", "78 8 19.050000 300.833333 3563.333333 13.266667 \n", " 6 19.066667 213.250000 3314.166667 16.391667 \n", " 5 20.300000 131.000000 2830.000000 15.900000 \n", " 4 29.576471 112.117647 2296.764706 16.282353 \n", "77 8 16.000000 335.750000 4177.500000 13.662500 \n", " 6 19.500000 220.400000 3383.000000 16.900000 \n", " 4 29.107143 106.500000 2205.071429 16.064286 \n", " 3 21.500000 80.000000 2720.000000 13.500000 \n", "76 8 14.666667 324.000000 4064.666667 13.222222 \n", " 6 20.000000 221.400000 3349.600000 17.000000 \n", " 4 26.766667 106.333333 2306.600000 16.866667 \n", "75 8 15.666667 330.500000 4108.833333 13.166667 \n", " 6 17.583333 233.750000 3398.333333 17.708333 \n", " 4 25.250000 114.833333 2489.250000 15.833333 \n", "74 8 14.200000 315.200000 4438.400000 14.700000 \n", " 6 17.857143 230.428571 3320.000000 16.857143 \n", " 4 27.800000 96.533333 2151.466667 16.400000 \n", "73 8 13.200000 365.250000 4279.050000 12.250000 \n", " 6 19.000000 212.250000 2917.125000 15.687500 \n", " 4 22.727273 109.272727 2338.090909 17.136364 \n", " 3 18.000000 70.000000 2124.000000 13.500000 \n", "72 8 13.615385 344.846154 4228.384615 13.000000 \n", " 4 23.428571 111.535714 2382.642857 17.214286 \n", " 3 19.000000 70.000000 2330.000000 13.500000 \n", "71 8 13.428571 371.714286 4537.714286 12.214286 \n", " 6 18.000000 243.375000 3171.875000 14.750000 \n", " 4 27.461538 101.846154 2056.384615 16.961538 \n", "70 8 14.111111 367.555556 3940.055556 11.194444 \n", " 6 20.500000 199.000000 2710.500000 15.500000 \n", " 4 25.285714 107.000000 2292.571429 16.000000 \n", "\n", " origin \n", "model_year cylinders \n", "82 6 1.000000 \n", " 4 1.714286 \n", "81 8 1.000000 \n", " 6 1.714286 \n", " 4 2.095238 \n", "80 6 2.000000 \n", " 5 2.000000 \n", " 4 2.200000 \n", " 3 3.000000 \n", "79 8 1.000000 \n", " 6 1.000000 \n", " 5 2.000000 \n", " 4 1.583333 \n", "78 8 1.000000 \n", " 6 1.166667 \n", " 5 2.000000 \n", " 4 2.117647 \n", "77 8 1.000000 \n", " 6 1.400000 \n", " 4 1.857143 \n", " 3 3.000000 \n", "76 8 1.000000 \n", " 6 1.300000 \n", " 4 1.866667 \n", "75 8 1.000000 \n", " 6 1.000000 \n", " 4 2.166667 \n", "74 8 1.000000 \n", " 6 1.000000 \n", " 4 2.200000 \n", "73 8 1.000000 \n", " 6 1.250000 \n", " 4 2.000000 \n", " 3 3.000000 \n", "72 8 1.000000 \n", " 4 1.928571 \n", " 3 3.000000 \n", "71 8 1.000000 \n", " 6 1.000000 \n", " 4 1.923077 \n", "70 8 1.000000 \n", " 6 1.000000 \n", " 4 2.285714 " ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.sort_index(level='model_year',ascending=False)" ] }, { "cell_type": "code", "execution_count": 29, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mpgdisplacementweightaccelerationorigin
model_yearcylinders
81826.600000350.0000003725.00000019.0000001.000000
79818.630000321.4000003862.90000015.4000001.000000
78819.050000300.8333333563.33333313.2666671.000000
77816.000000335.7500004177.50000013.6625001.000000
76814.666667324.0000004064.66666713.2222221.000000
75815.666667330.5000004108.83333313.1666671.000000
74814.200000315.2000004438.40000014.7000001.000000
73813.200000365.2500004279.05000012.2500001.000000
72813.615385344.8461544228.38461513.0000001.000000
71813.428571371.7142864537.71428612.2142861.000000
70814.111111367.5555563940.05555611.1944441.000000
82628.333333225.0000002931.66666716.0333331.000000
81623.428571184.0000003093.57142915.4428571.714286
80625.900000196.5000003145.50000015.0500002.000000
79622.950000205.6666673025.83333315.4333331.000000
78619.066667213.2500003314.16666716.3916671.166667
77619.500000220.4000003383.00000016.9000001.400000
76620.000000221.4000003349.60000017.0000001.300000
75617.583333233.7500003398.33333317.7083331.000000
74617.857143230.4285713320.00000016.8571431.000000
73619.000000212.2500002917.12500015.6875001.250000
71618.000000243.3750003171.87500014.7500001.000000
70620.500000199.0000002710.50000015.5000001.000000
80536.400000121.0000002950.00000019.9000002.000000
79525.400000183.0000003530.00000020.1000002.000000
78520.300000131.0000002830.00000015.9000002.000000
82432.071429118.5714292402.32142916.7035711.714286
81432.814286108.8571432275.47619016.4666672.095238
80434.612000111.0000002360.08000017.1440002.200000
79431.525000113.5833332357.58333315.9916671.583333
78429.576471112.1176472296.76470616.2823532.117647
77429.107143106.5000002205.07142916.0642861.857143
76426.766667106.3333332306.60000016.8666671.866667
75425.250000114.8333332489.25000015.8333332.166667
74427.80000096.5333332151.46666716.4000002.200000
73422.727273109.2727272338.09090917.1363642.000000
72423.428571111.5357142382.64285717.2142861.928571
71427.461538101.8461542056.38461516.9615381.923077
70425.285714107.0000002292.57142916.0000002.285714
80323.70000070.0000002420.00000012.5000003.000000
77321.50000080.0000002720.00000013.5000003.000000
73318.00000070.0000002124.00000013.5000003.000000
72319.00000070.0000002330.00000013.5000003.000000
\n", "
" ], "text/plain": [ " mpg displacement weight acceleration \\\n", "model_year cylinders \n", "81 8 26.600000 350.000000 3725.000000 19.000000 \n", "79 8 18.630000 321.400000 3862.900000 15.400000 \n", "78 8 19.050000 300.833333 3563.333333 13.266667 \n", "77 8 16.000000 335.750000 4177.500000 13.662500 \n", "76 8 14.666667 324.000000 4064.666667 13.222222 \n", "75 8 15.666667 330.500000 4108.833333 13.166667 \n", "74 8 14.200000 315.200000 4438.400000 14.700000 \n", "73 8 13.200000 365.250000 4279.050000 12.250000 \n", "72 8 13.615385 344.846154 4228.384615 13.000000 \n", "71 8 13.428571 371.714286 4537.714286 12.214286 \n", "70 8 14.111111 367.555556 3940.055556 11.194444 \n", "82 6 28.333333 225.000000 2931.666667 16.033333 \n", "81 6 23.428571 184.000000 3093.571429 15.442857 \n", "80 6 25.900000 196.500000 3145.500000 15.050000 \n", "79 6 22.950000 205.666667 3025.833333 15.433333 \n", "78 6 19.066667 213.250000 3314.166667 16.391667 \n", "77 6 19.500000 220.400000 3383.000000 16.900000 \n", "76 6 20.000000 221.400000 3349.600000 17.000000 \n", "75 6 17.583333 233.750000 3398.333333 17.708333 \n", "74 6 17.857143 230.428571 3320.000000 16.857143 \n", "73 6 19.000000 212.250000 2917.125000 15.687500 \n", "71 6 18.000000 243.375000 3171.875000 14.750000 \n", "70 6 20.500000 199.000000 2710.500000 15.500000 \n", "80 5 36.400000 121.000000 2950.000000 19.900000 \n", "79 5 25.400000 183.000000 3530.000000 20.100000 \n", "78 5 20.300000 131.000000 2830.000000 15.900000 \n", "82 4 32.071429 118.571429 2402.321429 16.703571 \n", "81 4 32.814286 108.857143 2275.476190 16.466667 \n", "80 4 34.612000 111.000000 2360.080000 17.144000 \n", "79 4 31.525000 113.583333 2357.583333 15.991667 \n", "78 4 29.576471 112.117647 2296.764706 16.282353 \n", "77 4 29.107143 106.500000 2205.071429 16.064286 \n", "76 4 26.766667 106.333333 2306.600000 16.866667 \n", "75 4 25.250000 114.833333 2489.250000 15.833333 \n", "74 4 27.800000 96.533333 2151.466667 16.400000 \n", "73 4 22.727273 109.272727 2338.090909 17.136364 \n", "72 4 23.428571 111.535714 2382.642857 17.214286 \n", "71 4 27.461538 101.846154 2056.384615 16.961538 \n", "70 4 25.285714 107.000000 2292.571429 16.000000 \n", "80 3 23.700000 70.000000 2420.000000 12.500000 \n", "77 3 21.500000 80.000000 2720.000000 13.500000 \n", "73 3 18.000000 70.000000 2124.000000 13.500000 \n", "72 3 19.000000 70.000000 2330.000000 13.500000 \n", "\n", " origin \n", "model_year cylinders \n", "81 8 1.000000 \n", "79 8 1.000000 \n", "78 8 1.000000 \n", "77 8 1.000000 \n", "76 8 1.000000 \n", "75 8 1.000000 \n", "74 8 1.000000 \n", "73 8 1.000000 \n", "72 8 1.000000 \n", "71 8 1.000000 \n", "70 8 1.000000 \n", "82 6 1.000000 \n", "81 6 1.714286 \n", "80 6 2.000000 \n", "79 6 1.000000 \n", "78 6 1.166667 \n", "77 6 1.400000 \n", "76 6 1.300000 \n", "75 6 1.000000 \n", "74 6 1.000000 \n", "73 6 1.250000 \n", "71 6 1.000000 \n", "70 6 1.000000 \n", "80 5 2.000000 \n", "79 5 2.000000 \n", "78 5 2.000000 \n", "82 4 1.714286 \n", "81 4 2.095238 \n", "80 4 2.200000 \n", "79 4 1.583333 \n", "78 4 2.117647 \n", "77 4 1.857143 \n", "76 4 1.866667 \n", "75 4 2.166667 \n", "74 4 2.200000 \n", "73 4 2.000000 \n", "72 4 1.928571 \n", "71 4 1.923077 \n", "70 4 2.285714 \n", "80 3 3.000000 \n", "77 3 3.000000 \n", "73 3 3.000000 \n", "72 3 3.000000 " ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "year_cyl.sort_index(level='cylinders',ascending=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Advanced: agg() method\n", "\n", "The agg() method allows you to customize what aggregate functions you want per category" ] }, { "cell_type": "code", "execution_count": 33, "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", "
mpgcylindersdisplacementhorsepowerweightaccelerationmodel_yearoriginname
018.08307.0130350412.0701chevrolet chevelle malibu
115.08350.0165369311.5701buick skylark 320
218.08318.0150343611.0701plymouth satellite
316.08304.0150343312.0701amc rebel sst
417.08302.0140344910.5701ford torino
..............................
39327.04140.086279015.6821ford mustang gl
39444.0497.052213024.6822vw pickup
39532.04135.084229511.6821dodge rampage
39628.04120.079262518.6821ford ranger
39731.04119.082272019.4821chevy s-10
\n", "

398 rows × 9 columns

\n", "
" ], "text/plain": [ " mpg cylinders displacement horsepower weight acceleration \\\n", "0 18.0 8 307.0 130 3504 12.0 \n", "1 15.0 8 350.0 165 3693 11.5 \n", "2 18.0 8 318.0 150 3436 11.0 \n", "3 16.0 8 304.0 150 3433 12.0 \n", "4 17.0 8 302.0 140 3449 10.5 \n", ".. ... ... ... ... ... ... \n", "393 27.0 4 140.0 86 2790 15.6 \n", "394 44.0 4 97.0 52 2130 24.6 \n", "395 32.0 4 135.0 84 2295 11.6 \n", "396 28.0 4 120.0 79 2625 18.6 \n", "397 31.0 4 119.0 82 2720 19.4 \n", "\n", " model_year origin name \n", "0 70 1 chevrolet chevelle malibu \n", "1 70 1 buick skylark 320 \n", "2 70 1 plymouth satellite \n", "3 70 1 amc rebel sst \n", "4 70 1 ford torino \n", ".. ... ... ... \n", "393 82 1 ford mustang gl \n", "394 82 2 vw pickup \n", "395 82 1 dodge rampage \n", "396 82 1 ford ranger \n", "397 82 1 chevy s-10 \n", "\n", "[398 rows x 9 columns]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## agg() on a DataFrame" ] }, { "cell_type": "code", "execution_count": 35, "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", "
mpgcylindersdisplacementweightaccelerationmodel_yearorigin
median23.0000004.000000148.5000002803.50000015.5000076.000001.000000
mean23.5145735.454774193.4258792970.42462315.5680976.010051.572864
\n", "
" ], "text/plain": [ " mpg cylinders displacement weight acceleration \\\n", "median 23.000000 4.000000 148.500000 2803.500000 15.50000 \n", "mean 23.514573 5.454774 193.425879 2970.424623 15.56809 \n", "\n", " model_year origin \n", "median 76.00000 1.000000 \n", "mean 76.01005 1.572864 " ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# These strings need to match up with built-in method names\n", "df.agg(['median','mean'])" ] }, { "cell_type": "code", "execution_count": 41, "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", "
mpgweight
sum9358.8000001.182229e+06
mean23.5145732.970425e+03
\n", "
" ], "text/plain": [ " mpg weight\n", "sum 9358.800000 1.182229e+06\n", "mean 23.514573 2.970425e+03" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.agg(['sum','mean'])[['mpg','weight']]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Specify aggregate methods per column\n", "\n", "**agg()** is very powerful,allowing you to pass in a dictionary where the keys are the columns and the values are a list of aggregate methods." ] }, { "cell_type": "code", "execution_count": 43, "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", "
mpgweight
mean23.5145732970.424623
median23.000000NaN
stdNaN846.841774
\n", "
" ], "text/plain": [ " mpg weight\n", "mean 23.514573 2970.424623\n", "median 23.000000 NaN\n", "std NaN 846.841774" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.agg({'mpg':['median','mean'],'weight':['mean','std']})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## agg() with groupby()" ] }, { "cell_type": "code", "execution_count": 44, "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", "
mpgweight
medianmeanmeanstd
model_year
7016.0017.6896553372.793103852.868663
7119.0021.2500002995.4285711061.830859
7218.5018.7142863237.714286974.520960
7316.0017.1000003419.025000974.809133
7424.0022.7037042877.925926949.308571
7519.5020.2666673176.800000765.179781
7621.0021.5735293078.735294821.371481
7721.7523.3750002997.357143912.825902
7820.7024.0611112861.805556626.023907
7923.9025.0931033055.344828747.881497
8032.7033.6965522436.655172432.235491
8131.6030.3344832522.931034533.600501
8232.0031.7096772453.548387354.276713
\n", "
" ], "text/plain": [ " mpg weight \n", " median mean mean std\n", "model_year \n", "70 16.00 17.689655 3372.793103 852.868663\n", "71 19.00 21.250000 2995.428571 1061.830859\n", "72 18.50 18.714286 3237.714286 974.520960\n", "73 16.00 17.100000 3419.025000 974.809133\n", "74 24.00 22.703704 2877.925926 949.308571\n", "75 19.50 20.266667 3176.800000 765.179781\n", "76 21.00 21.573529 3078.735294 821.371481\n", "77 21.75 23.375000 2997.357143 912.825902\n", "78 20.70 24.061111 2861.805556 626.023907\n", "79 23.90 25.093103 3055.344828 747.881497\n", "80 32.70 33.696552 2436.655172 432.235491\n", "81 31.60 30.334483 2522.931034 533.600501\n", "82 32.00 31.709677 2453.548387 354.276713" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.groupby('model_year').agg({'mpg':['median','mean'],'weight':['mean','std']})" ] } ], "metadata": { "anaconda-cloud": {}, "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.7.6" } }, "nbformat": 4, "nbformat_minor": 1 }