You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3004 lines
99 KiB

2 years ago
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"___\n",
"\n",
"<a href='http://www.pieriandata.com'><img src='../Pierian_Data_Logo.png'/></a>\n",
"___\n",
"<center><em>Copyright by Pierian Data Inc.</em></center>\n",
"<center><em>For more information, visit us at <a href='http://www.pieriandata.com'>www.pieriandata.com</a></em></center>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pivot Tables\n",
"\n",
"Pivoting data can sometimes help clarify relationships and connections.\n",
"\n",
"Full documentation on a variety of related pivot methods: https://pandas.pydata.org/docs/user_guide/reshaping.html\n",
"\n",
"## Data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = pd.read_csv('Sales_Funnel_CRM.csv')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Account Number</th>\n",
" <th>Company</th>\n",
" <th>Contact</th>\n",
" <th>Account Manager</th>\n",
" <th>Product</th>\n",
" <th>Licenses</th>\n",
" <th>Sale Price</th>\n",
" <th>Status</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2123398</td>\n",
" <td>Google</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2100000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2123398</td>\n",
" <td>Google</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2123398</td>\n",
" <td>Google</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Tracking</td>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2192650</td>\n",
" <td>BOBO</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" <td>Lost</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>420496</td>\n",
" <td>IKEA</td>\n",
" <td>Elon Tusk</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>636685</td>\n",
" <td>Tesla Inc.</td>\n",
" <td>Elon Tusk</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>2800000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>636685</td>\n",
" <td>Tesla Inc.</td>\n",
" <td>Elon Tusk</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>1216870</td>\n",
" <td>Microsoft</td>\n",
" <td>Will Grates</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Tracking</td>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>2200450</td>\n",
" <td>Walmart</td>\n",
" <td>Will Grates</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" <td>Lost</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>405886</td>\n",
" <td>Apple</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>470248</td>\n",
" <td>Exxon Mobile</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2100000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>698032</td>\n",
" <td>ATT</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Tracking</td>\n",
" <td>150</td>\n",
" <td>350000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>698032</td>\n",
" <td>ATT</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>902797</td>\n",
" <td>CVS Health</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Tracking</td>\n",
" <td>450</td>\n",
" <td>490000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>2046943</td>\n",
" <td>Salesforce</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>750</td>\n",
" <td>7000000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>2169499</td>\n",
" <td>Cisco</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" <td>Lost</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>2169499</td>\n",
" <td>Cisco</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>GPS Positioning</td>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Account Number Company Contact Account Manager \\\n",
"0 2123398 Google Larry Pager Edward Thorp \n",
"1 2123398 Google Larry Pager Edward Thorp \n",
"2 2123398 Google Larry Pager Edward Thorp \n",
"3 2192650 BOBO Larry Pager Edward Thorp \n",
"4 420496 IKEA Elon Tusk Edward Thorp \n",
"5 636685 Tesla Inc. Elon Tusk Edward Thorp \n",
"6 636685 Tesla Inc. Elon Tusk Edward Thorp \n",
"7 1216870 Microsoft Will Grates Edward Thorp \n",
"8 2200450 Walmart Will Grates Edward Thorp \n",
"9 405886 Apple Cindy Phoner Claude Shannon \n",
"10 470248 Exxon Mobile Cindy Phoner Claude Shannon \n",
"11 698032 ATT Cindy Phoner Claude Shannon \n",
"12 698032 ATT Cindy Phoner Claude Shannon \n",
"13 902797 CVS Health Emma Gordian Claude Shannon \n",
"14 2046943 Salesforce Emma Gordian Claude Shannon \n",
"15 2169499 Cisco Emma Gordian Claude Shannon \n",
"16 2169499 Cisco Emma Gordian Claude Shannon \n",
"\n",
" Product Licenses Sale Price Status \n",
"0 Analytics 150 2100000 Presented \n",
"1 Prediction 150 700000 Presented \n",
"2 Tracking 300 350000 Under Review \n",
"3 Analytics 150 2450000 Lost \n",
"4 Analytics 300 4550000 Won \n",
"5 Analytics 300 2800000 Under Review \n",
"6 Prediction 150 700000 Presented \n",
"7 Tracking 300 350000 Under Review \n",
"8 Analytics 150 2450000 Lost \n",
"9 Analytics 300 4550000 Won \n",
"10 Analytics 150 2100000 Presented \n",
"11 Tracking 150 350000 Under Review \n",
"12 Prediction 150 700000 Presented \n",
"13 Tracking 450 490000 Won \n",
"14 Analytics 750 7000000 Won \n",
"15 Analytics 300 4550000 Lost \n",
"16 GPS Positioning 300 350000 Presented "
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The pivot() method\n",
"\n",
"The pivot method reshapes data based on column values and reassignment of the index. Keep in mind, it doesn't always make sense to pivot data. In our machine learning lessons, we will see that our data doesn't need to be pivoted. Pivot methods are mainly for data analysis,visualization, and exploration.\n",
"\n",
"----\n",
"\n",
"Here is an image showing the idea behind a pivot() call:\n",
"\n",
"<img src='reshaping_pivot.png'>"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function pivot in module pandas.core.reshape.pivot:\n",
"\n",
"pivot(data:'DataFrame', index=None, columns=None, values=None) -> 'DataFrame'\n",
" Return reshaped DataFrame organized by given index / column values.\n",
" \n",
" Reshape data (produce a \"pivot\" table) based on column values. Uses\n",
" unique values from specified `index` / `columns` to form axes of the\n",
" resulting DataFrame. This function does not support data\n",
" aggregation, multiple values will result in a MultiIndex in the\n",
" columns. See the :ref:`User Guide <reshaping>` for more on reshaping.\n",
" \n",
" Parameters\n",
" ----------\n",
" data : DataFrame\n",
" index : str or object, optional\n",
" Column to use to make new frame's index. If None, uses\n",
" existing index.\n",
" columns : str or object\n",
" Column to use to make new frame's columns.\n",
" values : str, object or a list of the previous, optional\n",
" Column(s) to use for populating new frame's values. If not\n",
" specified, all remaining columns will be used and the result will\n",
" have hierarchically indexed columns.\n",
" \n",
" .. versionchanged:: 0.23.0\n",
" Also accept list of column names.\n",
" \n",
" Returns\n",
" -------\n",
" DataFrame\n",
" Returns reshaped DataFrame.\n",
" \n",
" Raises\n",
" ------\n",
" ValueError:\n",
" When there are any `index`, `columns` combinations with multiple\n",
" values. `DataFrame.pivot_table` when you need to aggregate.\n",
" \n",
" See Also\n",
" --------\n",
" DataFrame.pivot_table : Generalization of pivot that can handle\n",
" duplicate values for one index/column pair.\n",
" DataFrame.unstack : Pivot based on the index values instead of a\n",
" column.\n",
" \n",
" Notes\n",
" -----\n",
" For finer-tuned control, see hierarchical indexing documentation along\n",
" with the related stack/unstack methods.\n",
" \n",
" Examples\n",
" --------\n",
" >>> df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',\n",
" ... 'two'],\n",
" ... 'bar': ['A', 'B', 'C', 'A', 'B', 'C'],\n",
" ... 'baz': [1, 2, 3, 4, 5, 6],\n",
" ... 'zoo': ['x', 'y', 'z', 'q', 'w', 't']})\n",
" >>> df\n",
" foo bar baz zoo\n",
" 0 one A 1 x\n",
" 1 one B 2 y\n",
" 2 one C 3 z\n",
" 3 two A 4 q\n",
" 4 two B 5 w\n",
" 5 two C 6 t\n",
" \n",
" >>> df.pivot(index='foo', columns='bar', values='baz')\n",
" bar A B C\n",
" foo\n",
" one 1 2 3\n",
" two 4 5 6\n",
" \n",
" >>> df.pivot(index='foo', columns='bar')['baz']\n",
" bar A B C\n",
" foo\n",
" one 1 2 3\n",
" two 4 5 6\n",
" \n",
" >>> df.pivot(index='foo', columns='bar', values=['baz', 'zoo'])\n",
" baz zoo\n",
" bar A B C A B C\n",
" foo\n",
" one 1 2 3 x y z\n",
" two 4 5 6 q w t\n",
" \n",
" A ValueError is raised if there are any duplicates.\n",
" \n",
" >>> df = pd.DataFrame({\"foo\": ['one', 'one', 'two', 'two'],\n",
" ... \"bar\": ['A', 'A', 'B', 'C'],\n",
" ... \"baz\": [1, 2, 3, 4]})\n",
" >>> df\n",
" foo bar baz\n",
" 0 one A 1\n",
" 1 one A 2\n",
" 2 two B 3\n",
" 3 two C 4\n",
" \n",
" Notice that the first two rows are the same for our `index`\n",
" and `columns` arguments.\n",
" \n",
" >>> df.pivot(index='foo', columns='bar', values='baz')\n",
" Traceback (most recent call last):\n",
" ...\n",
" ValueError: Index contains duplicate entries, cannot reshape\n",
"\n"
]
}
],
"source": [
"help(pd.pivot)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"#### Note: Common Point of Confusion: Students often just randomly pass in index,column, and value choices in an attempt to see the changes. This often just leads to formatting errors. You should first go through this checklist BEFORE running a pivot():\n",
"\n",
"* What question are you trying to answer?\n",
"* What would a dataframe that answers the question look like? Does it need a pivot()\n",
"* What you want the resulting pivot to look like? Do you need all the original columns?\n",
"\n",
"-----"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Account Number</th>\n",
" <th>Company</th>\n",
" <th>Contact</th>\n",
" <th>Account Manager</th>\n",
" <th>Product</th>\n",
" <th>Licenses</th>\n",
" <th>Sale Price</th>\n",
" <th>Status</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2123398</td>\n",
" <td>Google</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2100000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2123398</td>\n",
" <td>Google</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2123398</td>\n",
" <td>Google</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Tracking</td>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2192650</td>\n",
" <td>BOBO</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" <td>Lost</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>420496</td>\n",
" <td>IKEA</td>\n",
" <td>Elon Tusk</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>636685</td>\n",
" <td>Tesla Inc.</td>\n",
" <td>Elon Tusk</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>2800000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>636685</td>\n",
" <td>Tesla Inc.</td>\n",
" <td>Elon Tusk</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>1216870</td>\n",
" <td>Microsoft</td>\n",
" <td>Will Grates</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Tracking</td>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>2200450</td>\n",
" <td>Walmart</td>\n",
" <td>Will Grates</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" <td>Lost</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>405886</td>\n",
" <td>Apple</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>470248</td>\n",
" <td>Exxon Mobile</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2100000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>698032</td>\n",
" <td>ATT</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Tracking</td>\n",
" <td>150</td>\n",
" <td>350000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>698032</td>\n",
" <td>ATT</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>902797</td>\n",
" <td>CVS Health</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Tracking</td>\n",
" <td>450</td>\n",
" <td>490000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>2046943</td>\n",
" <td>Salesforce</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>750</td>\n",
" <td>7000000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>2169499</td>\n",
" <td>Cisco</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" <td>Lost</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>2169499</td>\n",
" <td>Cisco</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>GPS Positioning</td>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Account Number Company Contact Account Manager \\\n",
"0 2123398 Google Larry Pager Edward Thorp \n",
"1 2123398 Google Larry Pager Edward Thorp \n",
"2 2123398 Google Larry Pager Edward Thorp \n",
"3 2192650 BOBO Larry Pager Edward Thorp \n",
"4 420496 IKEA Elon Tusk Edward Thorp \n",
"5 636685 Tesla Inc. Elon Tusk Edward Thorp \n",
"6 636685 Tesla Inc. Elon Tusk Edward Thorp \n",
"7 1216870 Microsoft Will Grates Edward Thorp \n",
"8 2200450 Walmart Will Grates Edward Thorp \n",
"9 405886 Apple Cindy Phoner Claude Shannon \n",
"10 470248 Exxon Mobile Cindy Phoner Claude Shannon \n",
"11 698032 ATT Cindy Phoner Claude Shannon \n",
"12 698032 ATT Cindy Phoner Claude Shannon \n",
"13 902797 CVS Health Emma Gordian Claude Shannon \n",
"14 2046943 Salesforce Emma Gordian Claude Shannon \n",
"15 2169499 Cisco Emma Gordian Claude Shannon \n",
"16 2169499 Cisco Emma Gordian Claude Shannon \n",
"\n",
" Product Licenses Sale Price Status \n",
"0 Analytics 150 2100000 Presented \n",
"1 Prediction 150 700000 Presented \n",
"2 Tracking 300 350000 Under Review \n",
"3 Analytics 150 2450000 Lost \n",
"4 Analytics 300 4550000 Won \n",
"5 Analytics 300 2800000 Under Review \n",
"6 Prediction 150 700000 Presented \n",
"7 Tracking 300 350000 Under Review \n",
"8 Analytics 150 2450000 Lost \n",
"9 Analytics 300 4550000 Won \n",
"10 Analytics 150 2100000 Presented \n",
"11 Tracking 150 350000 Under Review \n",
"12 Prediction 150 700000 Presented \n",
"13 Tracking 450 490000 Won \n",
"14 Analytics 750 7000000 Won \n",
"15 Analytics 300 4550000 Lost \n",
"16 GPS Positioning 300 350000 Presented "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------\n",
"** What type of question does a pivot help answer?**\n",
"\n",
"**Imagine we wanted to know, how many licenses of each product type did Google purchase? Currently the way the data is formatted is hard to read. Let's pivot it so this is clearer, we will take a subset of the data for the question at hand.**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Company</th>\n",
" <th>Product</th>\n",
" <th>Licenses</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Google</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Google</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Google</td>\n",
" <td>Tracking</td>\n",
" <td>300</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>BOBO</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>IKEA</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Tesla Inc.</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Tesla Inc.</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Microsoft</td>\n",
" <td>Tracking</td>\n",
" <td>300</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Walmart</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Apple</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>Exxon Mobile</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>ATT</td>\n",
" <td>Tracking</td>\n",
" <td>150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>ATT</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>CVS Health</td>\n",
" <td>Tracking</td>\n",
" <td>450</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>Salesforce</td>\n",
" <td>Analytics</td>\n",
" <td>750</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>Cisco</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>Cisco</td>\n",
" <td>GPS Positioning</td>\n",
" <td>300</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Company Product Licenses\n",
"0 Google Analytics 150\n",
"1 Google Prediction 150\n",
"2 Google Tracking 300\n",
"3 BOBO Analytics 150\n",
"4 IKEA Analytics 300\n",
"5 Tesla Inc. Analytics 300\n",
"6 Tesla Inc. Prediction 150\n",
"7 Microsoft Tracking 300\n",
"8 Walmart Analytics 150\n",
"9 Apple Analytics 300\n",
"10 Exxon Mobile Analytics 150\n",
"11 ATT Tracking 150\n",
"12 ATT Prediction 150\n",
"13 CVS Health Tracking 450\n",
"14 Salesforce Analytics 750\n",
"15 Cisco Analytics 300\n",
"16 Cisco GPS Positioning 300"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let's take a subset, otherwise we'll get an error due to duplicate rows and data\n",
"licenses = df[['Company','Product','Licenses']]\n",
"licenses"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>Product</th>\n",
" <th>Analytics</th>\n",
" <th>GPS Positioning</th>\n",
" <th>Prediction</th>\n",
" <th>Tracking</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Company</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Google</th>\n",
" <td>150.0</td>\n",
" <td>NaN</td>\n",
" <td>150.0</td>\n",
" <td>300.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ATT</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>150.0</td>\n",
" <td>150.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Apple</th>\n",
" <td>300.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BOBO</th>\n",
" <td>150.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>CVS Health</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>450.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Cisco</th>\n",
" <td>300.0</td>\n",
" <td>300.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Exxon Mobile</th>\n",
" <td>150.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>IKEA</th>\n",
" <td>300.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Microsoft</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>300.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Salesforce</th>\n",
" <td>750.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tesla Inc.</th>\n",
" <td>300.0</td>\n",
" <td>NaN</td>\n",
" <td>150.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Walmart</th>\n",
" <td>150.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"Product Analytics GPS Positioning Prediction Tracking\n",
"Company \n",
" Google 150.0 NaN 150.0 300.0\n",
"ATT NaN NaN 150.0 150.0\n",
"Apple 300.0 NaN NaN NaN\n",
"BOBO 150.0 NaN NaN NaN\n",
"CVS Health NaN NaN NaN 450.0\n",
"Cisco 300.0 300.0 NaN NaN\n",
"Exxon Mobile 150.0 NaN NaN NaN\n",
"IKEA 300.0 NaN NaN NaN\n",
"Microsoft NaN NaN NaN 300.0\n",
"Salesforce 750.0 NaN NaN NaN\n",
"Tesla Inc. 300.0 NaN 150.0 NaN\n",
"Walmart 150.0 NaN NaN NaN"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.pivot(data=licenses,index='Company',columns='Product',values='Licenses')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The pivot_table() method\n",
"\n",
"Similar to the pivot() method, the pivot_table() can add aggregation functions to a pivot call."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Account Number</th>\n",
" <th>Company</th>\n",
" <th>Contact</th>\n",
" <th>Account Manager</th>\n",
" <th>Product</th>\n",
" <th>Licenses</th>\n",
" <th>Sale Price</th>\n",
" <th>Status</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2123398</td>\n",
" <td>Google</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2100000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2123398</td>\n",
" <td>Google</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2123398</td>\n",
" <td>Google</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Tracking</td>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2192650</td>\n",
" <td>BOBO</td>\n",
" <td>Larry Pager</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" <td>Lost</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>420496</td>\n",
" <td>IKEA</td>\n",
" <td>Elon Tusk</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>636685</td>\n",
" <td>Tesla Inc.</td>\n",
" <td>Elon Tusk</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>2800000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>636685</td>\n",
" <td>Tesla Inc.</td>\n",
" <td>Elon Tusk</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>1216870</td>\n",
" <td>Microsoft</td>\n",
" <td>Will Grates</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Tracking</td>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>2200450</td>\n",
" <td>Walmart</td>\n",
" <td>Will Grates</td>\n",
" <td>Edward Thorp</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" <td>Lost</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>405886</td>\n",
" <td>Apple</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>470248</td>\n",
" <td>Exxon Mobile</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>150</td>\n",
" <td>2100000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>698032</td>\n",
" <td>ATT</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Tracking</td>\n",
" <td>150</td>\n",
" <td>350000</td>\n",
" <td>Under Review</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>698032</td>\n",
" <td>ATT</td>\n",
" <td>Cindy Phoner</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Prediction</td>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>902797</td>\n",
" <td>CVS Health</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Tracking</td>\n",
" <td>450</td>\n",
" <td>490000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>2046943</td>\n",
" <td>Salesforce</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>750</td>\n",
" <td>7000000</td>\n",
" <td>Won</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>2169499</td>\n",
" <td>Cisco</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>Analytics</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" <td>Lost</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>2169499</td>\n",
" <td>Cisco</td>\n",
" <td>Emma Gordian</td>\n",
" <td>Claude Shannon</td>\n",
" <td>GPS Positioning</td>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" <td>Presented</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Account Number Company Contact Account Manager \\\n",
"0 2123398 Google Larry Pager Edward Thorp \n",
"1 2123398 Google Larry Pager Edward Thorp \n",
"2 2123398 Google Larry Pager Edward Thorp \n",
"3 2192650 BOBO Larry Pager Edward Thorp \n",
"4 420496 IKEA Elon Tusk Edward Thorp \n",
"5 636685 Tesla Inc. Elon Tusk Edward Thorp \n",
"6 636685 Tesla Inc. Elon Tusk Edward Thorp \n",
"7 1216870 Microsoft Will Grates Edward Thorp \n",
"8 2200450 Walmart Will Grates Edward Thorp \n",
"9 405886 Apple Cindy Phoner Claude Shannon \n",
"10 470248 Exxon Mobile Cindy Phoner Claude Shannon \n",
"11 698032 ATT Cindy Phoner Claude Shannon \n",
"12 698032 ATT Cindy Phoner Claude Shannon \n",
"13 902797 CVS Health Emma Gordian Claude Shannon \n",
"14 2046943 Salesforce Emma Gordian Claude Shannon \n",
"15 2169499 Cisco Emma Gordian Claude Shannon \n",
"16 2169499 Cisco Emma Gordian Claude Shannon \n",
"\n",
" Product Licenses Sale Price Status \n",
"0 Analytics 150 2100000 Presented \n",
"1 Prediction 150 700000 Presented \n",
"2 Tracking 300 350000 Under Review \n",
"3 Analytics 150 2450000 Lost \n",
"4 Analytics 300 4550000 Won \n",
"5 Analytics 300 2800000 Under Review \n",
"6 Prediction 150 700000 Presented \n",
"7 Tracking 300 350000 Under Review \n",
"8 Analytics 150 2450000 Lost \n",
"9 Analytics 300 4550000 Won \n",
"10 Analytics 150 2100000 Presented \n",
"11 Tracking 150 350000 Under Review \n",
"12 Prediction 150 700000 Presented \n",
"13 Tracking 450 490000 Won \n",
"14 Analytics 750 7000000 Won \n",
"15 Analytics 300 4550000 Lost \n",
"16 GPS Positioning 300 350000 Presented "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Account Number</th>\n",
" <th>Licenses</th>\n",
" <th>Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Company</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Google</th>\n",
" <td>6370194</td>\n",
" <td>600</td>\n",
" <td>3150000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ATT</th>\n",
" <td>1396064</td>\n",
" <td>300</td>\n",
" <td>1050000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Apple</th>\n",
" <td>405886</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BOBO</th>\n",
" <td>2192650</td>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>CVS Health</th>\n",
" <td>902797</td>\n",
" <td>450</td>\n",
" <td>490000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Cisco</th>\n",
" <td>4338998</td>\n",
" <td>600</td>\n",
" <td>4900000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Exxon Mobile</th>\n",
" <td>470248</td>\n",
" <td>150</td>\n",
" <td>2100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>IKEA</th>\n",
" <td>420496</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Microsoft</th>\n",
" <td>1216870</td>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Salesforce</th>\n",
" <td>2046943</td>\n",
" <td>750</td>\n",
" <td>7000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tesla Inc.</th>\n",
" <td>1273370</td>\n",
" <td>450</td>\n",
" <td>3500000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Walmart</th>\n",
" <td>2200450</td>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Account Number Licenses Sale Price\n",
"Company \n",
" Google 6370194 600 3150000\n",
"ATT 1396064 300 1050000\n",
"Apple 405886 300 4550000\n",
"BOBO 2192650 150 2450000\n",
"CVS Health 902797 450 490000\n",
"Cisco 4338998 600 4900000\n",
"Exxon Mobile 470248 150 2100000\n",
"IKEA 420496 300 4550000\n",
"Microsoft 1216870 300 350000\n",
"Salesforce 2046943 750 7000000\n",
"Tesla Inc. 1273370 450 3500000\n",
"Walmart 2200450 150 2450000"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Notice Account Number sum() doesn't make sense to keep/use\n",
"pd.pivot_table(df,index=\"Company\",aggfunc='sum')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Licenses</th>\n",
" <th>Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Company</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Google</th>\n",
" <td>600</td>\n",
" <td>3150000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ATT</th>\n",
" <td>300</td>\n",
" <td>1050000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Apple</th>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BOBO</th>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>CVS Health</th>\n",
" <td>450</td>\n",
" <td>490000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Cisco</th>\n",
" <td>600</td>\n",
" <td>4900000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Exxon Mobile</th>\n",
" <td>150</td>\n",
" <td>2100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>IKEA</th>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Microsoft</th>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Salesforce</th>\n",
" <td>750</td>\n",
" <td>7000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tesla Inc.</th>\n",
" <td>450</td>\n",
" <td>3500000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Walmart</th>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Licenses Sale Price\n",
"Company \n",
" Google 600 3150000\n",
"ATT 300 1050000\n",
"Apple 300 4550000\n",
"BOBO 150 2450000\n",
"CVS Health 450 490000\n",
"Cisco 600 4900000\n",
"Exxon Mobile 150 2100000\n",
"IKEA 300 4550000\n",
"Microsoft 300 350000\n",
"Salesforce 750 7000000\n",
"Tesla Inc. 450 3500000\n",
"Walmart 150 2450000"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Either grab the columns\n",
"pd.pivot_table(df,index=\"Company\",aggfunc='sum')[['Licenses','Sale Price']]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Licenses</th>\n",
" <th>Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Company</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Google</th>\n",
" <td>600</td>\n",
" <td>3150000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ATT</th>\n",
" <td>300</td>\n",
" <td>1050000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Apple</th>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BOBO</th>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>CVS Health</th>\n",
" <td>450</td>\n",
" <td>490000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Cisco</th>\n",
" <td>600</td>\n",
" <td>4900000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Exxon Mobile</th>\n",
" <td>150</td>\n",
" <td>2100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>IKEA</th>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Microsoft</th>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Salesforce</th>\n",
" <td>750</td>\n",
" <td>7000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tesla Inc.</th>\n",
" <td>450</td>\n",
" <td>3500000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Walmart</th>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Licenses Sale Price\n",
"Company \n",
" Google 600 3150000\n",
"ATT 300 1050000\n",
"Apple 300 4550000\n",
"BOBO 150 2450000\n",
"CVS Health 450 490000\n",
"Cisco 600 4900000\n",
"Exxon Mobile 150 2100000\n",
"IKEA 300 4550000\n",
"Microsoft 300 350000\n",
"Salesforce 750 7000000\n",
"Tesla Inc. 450 3500000\n",
"Walmart 150 2450000"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Or state them as wanted values\n",
"pd.pivot_table(df,index=\"Company\",aggfunc='sum',values=['Licenses','Sale Price'])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Licenses</th>\n",
" <th>Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Company</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Google</th>\n",
" <td>600</td>\n",
" <td>3150000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ATT</th>\n",
" <td>300</td>\n",
" <td>1050000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Apple</th>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BOBO</th>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>CVS Health</th>\n",
" <td>450</td>\n",
" <td>490000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Cisco</th>\n",
" <td>600</td>\n",
" <td>4900000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Exxon Mobile</th>\n",
" <td>150</td>\n",
" <td>2100000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>IKEA</th>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Microsoft</th>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Salesforce</th>\n",
" <td>750</td>\n",
" <td>7000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tesla Inc.</th>\n",
" <td>450</td>\n",
" <td>3500000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Walmart</th>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Licenses Sale Price\n",
"Company \n",
" Google 600 3150000\n",
"ATT 300 1050000\n",
"Apple 300 4550000\n",
"BOBO 150 2450000\n",
"CVS Health 450 490000\n",
"Cisco 600 4900000\n",
"Exxon Mobile 150 2100000\n",
"IKEA 300 4550000\n",
"Microsoft 300 350000\n",
"Salesforce 750 7000000\n",
"Tesla Inc. 450 3500000\n",
"Walmart 150 2450000"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.groupby('Company').sum()[['Licenses','Sale Price']]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Account Manager</th>\n",
" <th>Contact</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">Claude Shannon</th>\n",
" <th>Cindy Phoner</th>\n",
" <td>7700000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Emma Gordian</th>\n",
" <td>12390000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Edward Thorp</th>\n",
" <th>Elon Tusk</th>\n",
" <td>8050000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Larry Pager</th>\n",
" <td>5600000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Will Grates</th>\n",
" <td>2800000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Sale Price\n",
"Account Manager Contact \n",
"Claude Shannon Cindy Phoner 7700000\n",
" Emma Gordian 12390000\n",
"Edward Thorp Elon Tusk 8050000\n",
" Larry Pager 5600000\n",
" Will Grates 2800000"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.pivot_table(df,index=[\"Account Manager\",\"Contact\"],values=['Sale Price'],aggfunc='sum')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Columns are optional - they provide an additional way to segment the actual values you care about. The aggregation functions are applied to the values you list."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe thead tr:last-of-type th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th colspan=\"4\" halign=\"left\">sum</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th colspan=\"4\" halign=\"left\">Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>Product</th>\n",
" <th>Analytics</th>\n",
" <th>GPS Positioning</th>\n",
" <th>Prediction</th>\n",
" <th>Tracking</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Account Manager</th>\n",
" <th>Contact</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">Claude Shannon</th>\n",
" <th>Cindy Phoner</th>\n",
" <td>6650000.0</td>\n",
" <td>NaN</td>\n",
" <td>700000.0</td>\n",
" <td>350000.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Emma Gordian</th>\n",
" <td>11550000.0</td>\n",
" <td>350000.0</td>\n",
" <td>NaN</td>\n",
" <td>490000.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Edward Thorp</th>\n",
" <th>Elon Tusk</th>\n",
" <td>7350000.0</td>\n",
" <td>NaN</td>\n",
" <td>700000.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Larry Pager</th>\n",
" <td>4550000.0</td>\n",
" <td>NaN</td>\n",
" <td>700000.0</td>\n",
" <td>350000.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Will Grates</th>\n",
" <td>2450000.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>350000.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sum \n",
" Sale Price \n",
"Product Analytics GPS Positioning Prediction Tracking\n",
"Account Manager Contact \n",
"Claude Shannon Cindy Phoner 6650000.0 NaN 700000.0 350000.0\n",
" Emma Gordian 11550000.0 350000.0 NaN 490000.0\n",
"Edward Thorp Elon Tusk 7350000.0 NaN 700000.0 NaN\n",
" Larry Pager 4550000.0 NaN 700000.0 350000.0\n",
" Will Grates 2450000.0 NaN NaN 350000.0"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.pivot_table(df,index=[\"Account Manager\",\"Contact\"],values=[\"Sale Price\"],columns=[\"Product\"],aggfunc=[np.sum])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe thead tr:last-of-type th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th colspan=\"4\" halign=\"left\">sum</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th colspan=\"4\" halign=\"left\">Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>Product</th>\n",
" <th>Analytics</th>\n",
" <th>GPS Positioning</th>\n",
" <th>Prediction</th>\n",
" <th>Tracking</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Account Manager</th>\n",
" <th>Contact</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">Claude Shannon</th>\n",
" <th>Cindy Phoner</th>\n",
" <td>6650000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Emma Gordian</th>\n",
" <td>11550000</td>\n",
" <td>350000</td>\n",
" <td>0</td>\n",
" <td>490000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Edward Thorp</th>\n",
" <th>Elon Tusk</th>\n",
" <td>7350000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Larry Pager</th>\n",
" <td>4550000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Will Grates</th>\n",
" <td>2450000</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sum \n",
" Sale Price \n",
"Product Analytics GPS Positioning Prediction Tracking\n",
"Account Manager Contact \n",
"Claude Shannon Cindy Phoner 6650000 0 700000 350000\n",
" Emma Gordian 11550000 350000 0 490000\n",
"Edward Thorp Elon Tusk 7350000 0 700000 0\n",
" Larry Pager 4550000 0 700000 350000\n",
" Will Grates 2450000 0 0 350000"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.pivot_table(df,index=[\"Account Manager\",\"Contact\"],values=[\"Sale Price\"],columns=[\"Product\"],aggfunc=[np.sum],fill_value=0)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe thead tr:last-of-type th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th colspan=\"4\" halign=\"left\">sum</th>\n",
" <th colspan=\"4\" halign=\"left\">mean</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th colspan=\"4\" halign=\"left\">Sale Price</th>\n",
" <th colspan=\"4\" halign=\"left\">Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>Product</th>\n",
" <th>Analytics</th>\n",
" <th>GPS Positioning</th>\n",
" <th>Prediction</th>\n",
" <th>Tracking</th>\n",
" <th>Analytics</th>\n",
" <th>GPS Positioning</th>\n",
" <th>Prediction</th>\n",
" <th>Tracking</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Account Manager</th>\n",
" <th>Contact</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">Claude Shannon</th>\n",
" <th>Cindy Phoner</th>\n",
" <td>6650000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>350000</td>\n",
" <td>3325000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Emma Gordian</th>\n",
" <td>11550000</td>\n",
" <td>350000</td>\n",
" <td>0</td>\n",
" <td>490000</td>\n",
" <td>5775000</td>\n",
" <td>350000</td>\n",
" <td>0</td>\n",
" <td>490000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Edward Thorp</th>\n",
" <th>Elon Tusk</th>\n",
" <td>7350000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>0</td>\n",
" <td>3675000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Larry Pager</th>\n",
" <td>4550000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>350000</td>\n",
" <td>2275000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Will Grates</th>\n",
" <td>2450000</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>350000</td>\n",
" <td>2450000</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sum \\\n",
" Sale Price \n",
"Product Analytics GPS Positioning Prediction Tracking \n",
"Account Manager Contact \n",
"Claude Shannon Cindy Phoner 6650000 0 700000 350000 \n",
" Emma Gordian 11550000 350000 0 490000 \n",
"Edward Thorp Elon Tusk 7350000 0 700000 0 \n",
" Larry Pager 4550000 0 700000 350000 \n",
" Will Grates 2450000 0 0 350000 \n",
"\n",
" mean \n",
" Sale Price \n",
"Product Analytics GPS Positioning Prediction Tracking \n",
"Account Manager Contact \n",
"Claude Shannon Cindy Phoner 3325000 0 700000 350000 \n",
" Emma Gordian 5775000 350000 0 490000 \n",
"Edward Thorp Elon Tusk 3675000 0 700000 0 \n",
" Larry Pager 2275000 0 700000 350000 \n",
" Will Grates 2450000 0 0 350000 "
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Can add multiple agg functions\n",
"pd.pivot_table(df,index=[\"Account Manager\",\"Contact\"],values=[\"Sale Price\"],columns=[\"Product\"],\n",
" aggfunc=[np.sum,np.mean],fill_value=0)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe thead tr:last-of-type th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th colspan=\"8\" halign=\"left\">sum</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th colspan=\"4\" halign=\"left\">Licenses</th>\n",
" <th colspan=\"4\" halign=\"left\">Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>Product</th>\n",
" <th>Analytics</th>\n",
" <th>GPS Positioning</th>\n",
" <th>Prediction</th>\n",
" <th>Tracking</th>\n",
" <th>Analytics</th>\n",
" <th>GPS Positioning</th>\n",
" <th>Prediction</th>\n",
" <th>Tracking</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Account Manager</th>\n",
" <th>Contact</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">Claude Shannon</th>\n",
" <th>Cindy Phoner</th>\n",
" <td>450</td>\n",
" <td>0</td>\n",
" <td>150</td>\n",
" <td>150</td>\n",
" <td>6650000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Emma Gordian</th>\n",
" <td>1050</td>\n",
" <td>300</td>\n",
" <td>0</td>\n",
" <td>450</td>\n",
" <td>11550000</td>\n",
" <td>350000</td>\n",
" <td>0</td>\n",
" <td>490000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Edward Thorp</th>\n",
" <th>Elon Tusk</th>\n",
" <td>600</td>\n",
" <td>0</td>\n",
" <td>150</td>\n",
" <td>0</td>\n",
" <td>7350000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Larry Pager</th>\n",
" <td>300</td>\n",
" <td>0</td>\n",
" <td>150</td>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" <td>0</td>\n",
" <td>700000</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Will Grates</th>\n",
" <td>150</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>300</td>\n",
" <td>2450000</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sum \\\n",
" Licenses \n",
"Product Analytics GPS Positioning Prediction Tracking \n",
"Account Manager Contact \n",
"Claude Shannon Cindy Phoner 450 0 150 150 \n",
" Emma Gordian 1050 300 0 450 \n",
"Edward Thorp Elon Tusk 600 0 150 0 \n",
" Larry Pager 300 0 150 300 \n",
" Will Grates 150 0 0 300 \n",
"\n",
" \n",
" Sale Price \n",
"Product Analytics GPS Positioning Prediction Tracking \n",
"Account Manager Contact \n",
"Claude Shannon Cindy Phoner 6650000 0 700000 350000 \n",
" Emma Gordian 11550000 350000 0 490000 \n",
"Edward Thorp Elon Tusk 7350000 0 700000 0 \n",
" Larry Pager 4550000 0 700000 350000 \n",
" Will Grates 2450000 0 0 350000 "
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Can add on multiple columns\n",
"pd.pivot_table(df,index=[\"Account Manager\",\"Contact\"],values=[\"Sale Price\",\"Licenses\"],columns=[\"Product\"],\n",
" aggfunc=[np.sum],fill_value=0)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe thead tr:last-of-type th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th colspan=\"2\" halign=\"left\">sum</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th>Licenses</th>\n",
" <th>Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Account Manager</th>\n",
" <th>Contact</th>\n",
" <th>Product</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"6\" valign=\"top\">Claude Shannon</th>\n",
" <th rowspan=\"3\" valign=\"top\">Cindy Phoner</th>\n",
" <th>Analytics</th>\n",
" <td>450</td>\n",
" <td>6650000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Prediction</th>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tracking</th>\n",
" <td>150</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Emma Gordian</th>\n",
" <th>Analytics</th>\n",
" <td>1050</td>\n",
" <td>11550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>GPS Positioning</th>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tracking</th>\n",
" <td>450</td>\n",
" <td>490000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"7\" valign=\"top\">Edward Thorp</th>\n",
" <th rowspan=\"2\" valign=\"top\">Elon Tusk</th>\n",
" <th>Analytics</th>\n",
" <td>600</td>\n",
" <td>7350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Prediction</th>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Larry Pager</th>\n",
" <th>Analytics</th>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Prediction</th>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tracking</th>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">Will Grates</th>\n",
" <th>Analytics</th>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tracking</th>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sum \n",
" Licenses Sale Price\n",
"Account Manager Contact Product \n",
"Claude Shannon Cindy Phoner Analytics 450 6650000\n",
" Prediction 150 700000\n",
" Tracking 150 350000\n",
" Emma Gordian Analytics 1050 11550000\n",
" GPS Positioning 300 350000\n",
" Tracking 450 490000\n",
"Edward Thorp Elon Tusk Analytics 600 7350000\n",
" Prediction 150 700000\n",
" Larry Pager Analytics 300 4550000\n",
" Prediction 150 700000\n",
" Tracking 300 350000\n",
" Will Grates Analytics 150 2450000\n",
" Tracking 300 350000"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Can add on multiple columns\n",
"pd.pivot_table(df,index=[\"Account Manager\",\"Contact\",\"Product\"],values=[\"Sale Price\",\"Licenses\"],\n",
" aggfunc=[np.sum],fill_value=0)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe thead tr:last-of-type th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th colspan=\"2\" halign=\"left\">sum</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th>Licenses</th>\n",
" <th>Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Account Manager</th>\n",
" <th>Contact</th>\n",
" <th>Product</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"6\" valign=\"top\">Claude Shannon</th>\n",
" <th rowspan=\"3\" valign=\"top\">Cindy Phoner</th>\n",
" <th>Analytics</th>\n",
" <td>450</td>\n",
" <td>6650000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Prediction</th>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tracking</th>\n",
" <td>150</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Emma Gordian</th>\n",
" <th>Analytics</th>\n",
" <td>1050</td>\n",
" <td>11550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>GPS Positioning</th>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tracking</th>\n",
" <td>450</td>\n",
" <td>490000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"7\" valign=\"top\">Edward Thorp</th>\n",
" <th rowspan=\"2\" valign=\"top\">Elon Tusk</th>\n",
" <th>Analytics</th>\n",
" <td>600</td>\n",
" <td>7350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Prediction</th>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Larry Pager</th>\n",
" <th>Analytics</th>\n",
" <td>300</td>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Prediction</th>\n",
" <td>150</td>\n",
" <td>700000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tracking</th>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">Will Grates</th>\n",
" <th>Analytics</th>\n",
" <td>150</td>\n",
" <td>2450000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tracking</th>\n",
" <td>300</td>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>All</th>\n",
" <th></th>\n",
" <th></th>\n",
" <td>4500</td>\n",
" <td>36540000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sum \n",
" Licenses Sale Price\n",
"Account Manager Contact Product \n",
"Claude Shannon Cindy Phoner Analytics 450 6650000\n",
" Prediction 150 700000\n",
" Tracking 150 350000\n",
" Emma Gordian Analytics 1050 11550000\n",
" GPS Positioning 300 350000\n",
" Tracking 450 490000\n",
"Edward Thorp Elon Tusk Analytics 600 7350000\n",
" Prediction 150 700000\n",
" Larry Pager Analytics 300 4550000\n",
" Prediction 150 700000\n",
" Tracking 300 350000\n",
" Will Grates Analytics 150 2450000\n",
" Tracking 300 350000\n",
"All 4500 36540000"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# get Final \"ALL\" with margins = True\n",
"# Can add on multiple columns\n",
"pd.pivot_table(df,index=[\"Account Manager\",\"Contact\",\"Product\"],values=[\"Sale Price\",\"Licenses\"],\n",
" aggfunc=[np.sum],fill_value=0,margins=True)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe thead tr:last-of-type th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th>sum</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th></th>\n",
" <th>Sale Price</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Account Manager</th>\n",
" <th>Status</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"4\" valign=\"top\">Claude Shannon</th>\n",
" <th>Lost</th>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Presented</th>\n",
" <td>3150000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Under Review</th>\n",
" <td>350000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Won</th>\n",
" <td>12040000</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"4\" valign=\"top\">Edward Thorp</th>\n",
" <th>Lost</th>\n",
" <td>4900000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Presented</th>\n",
" <td>3500000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Under Review</th>\n",
" <td>3500000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Won</th>\n",
" <td>4550000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>All</th>\n",
" <th></th>\n",
" <td>36540000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sum\n",
" Sale Price\n",
"Account Manager Status \n",
"Claude Shannon Lost 4550000\n",
" Presented 3150000\n",
" Under Review 350000\n",
" Won 12040000\n",
"Edward Thorp Lost 4900000\n",
" Presented 3500000\n",
" Under Review 3500000\n",
" Won 4550000\n",
"All 36540000"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.pivot_table(df,index=[\"Account Manager\",\"Status\"],values=[\"Sale Price\"],\n",
" aggfunc=[np.sum],fill_value=0,margins=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----"
]
}
],
"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
}