{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "title: \"What is the Difference Between axis=0 and axis=1 When Working with Pandas Dataframes?\"\n", "author: \"Andrew Bancroft\"\n", "date: 2019-05-22\n", "description: \"Provides an example scenario to help decipher the difference between axis=0 and axis=1 when working with Pandas dataframes\"\n", "type: technical_note\n", "draft: false\n", "comments: true\n", "wip: false\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes, functions ask you to specify an `axis`. The documentation can often feel vague and/or technical.\n", "\n", "For instance, here's a quote from the `apply` function's documentation:\n", " \n", "> axis : {0 or ‘index’, 1 or ‘columns’}, default 0\n", "Axis along which the function is applied:\n", "0 or ‘index’: apply function to each column.\n", "1 or ‘columns’: apply function to each row.\n", "\n", "Uuuum... right.\n", "\n", "So what's the difference? Here's an example..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", " | DealershipName | \n", "RedCars | \n", "SilverCars | \n", "BlackCars | \n", "BlueCars | \n", "MonthSold | \n", "YearSold | \n", "
---|---|---|---|---|---|---|---|
0 | \n", "Clyde's Clunkers | \n", "902.0 | \n", "650.0 | \n", "754.0 | \n", "792.0 | \n", "1.0 | \n", "2018.0 | \n", "
1 | \n", "Clyde's Clunkers | \n", "710.0 | \n", "476.0 | \n", "518.0 | \n", "492.0 | \n", "2.0 | \n", "2018.0 | \n", "
2 | \n", "Clyde's Clunkers | \n", "248.0 | \n", "912.0 | \n", "606.0 | \n", "350.0 | \n", "3.0 | \n", "2018.0 | \n", "
3 | \n", "Clyde's Clunkers | \n", "782.0 | \n", "912.0 | \n", "858.0 | \n", "446.0 | \n", "4.0 | \n", "2018.0 | \n", "
4 | \n", "Clyde's Clunkers | \n", "278.0 | \n", "354.0 | \n", "482.0 | \n", "752.0 | \n", "5.0 | \n", "2018.0 | \n", "