{ "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": [ "# 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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Account NumberCompanyContactAccount ManagerProductLicensesSale PriceStatus
02123398GoogleLarry PagerEdward ThorpAnalytics1502100000Presented
12123398GoogleLarry PagerEdward ThorpPrediction150700000Presented
22123398GoogleLarry PagerEdward ThorpTracking300350000Under Review
32192650BOBOLarry PagerEdward ThorpAnalytics1502450000Lost
4420496IKEAElon TuskEdward ThorpAnalytics3004550000Won
5636685Tesla Inc.Elon TuskEdward ThorpAnalytics3002800000Under Review
6636685Tesla Inc.Elon TuskEdward ThorpPrediction150700000Presented
71216870MicrosoftWill GratesEdward ThorpTracking300350000Under Review
82200450WalmartWill GratesEdward ThorpAnalytics1502450000Lost
9405886AppleCindy PhonerClaude ShannonAnalytics3004550000Won
10470248Exxon MobileCindy PhonerClaude ShannonAnalytics1502100000Presented
11698032ATTCindy PhonerClaude ShannonTracking150350000Under Review
12698032ATTCindy PhonerClaude ShannonPrediction150700000Presented
13902797CVS HealthEmma GordianClaude ShannonTracking450490000Won
142046943SalesforceEmma GordianClaude ShannonAnalytics7507000000Won
152169499CiscoEmma GordianClaude ShannonAnalytics3004550000Lost
162169499CiscoEmma GordianClaude ShannonGPS Positioning300350000Presented
\n", "
" ], "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", "" ] }, { "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 ` 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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Account NumberCompanyContactAccount ManagerProductLicensesSale PriceStatus
02123398GoogleLarry PagerEdward ThorpAnalytics1502100000Presented
12123398GoogleLarry PagerEdward ThorpPrediction150700000Presented
22123398GoogleLarry PagerEdward ThorpTracking300350000Under Review
32192650BOBOLarry PagerEdward ThorpAnalytics1502450000Lost
4420496IKEAElon TuskEdward ThorpAnalytics3004550000Won
5636685Tesla Inc.Elon TuskEdward ThorpAnalytics3002800000Under Review
6636685Tesla Inc.Elon TuskEdward ThorpPrediction150700000Presented
71216870MicrosoftWill GratesEdward ThorpTracking300350000Under Review
82200450WalmartWill GratesEdward ThorpAnalytics1502450000Lost
9405886AppleCindy PhonerClaude ShannonAnalytics3004550000Won
10470248Exxon MobileCindy PhonerClaude ShannonAnalytics1502100000Presented
11698032ATTCindy PhonerClaude ShannonTracking150350000Under Review
12698032ATTCindy PhonerClaude ShannonPrediction150700000Presented
13902797CVS HealthEmma GordianClaude ShannonTracking450490000Won
142046943SalesforceEmma GordianClaude ShannonAnalytics7507000000Won
152169499CiscoEmma GordianClaude ShannonAnalytics3004550000Lost
162169499CiscoEmma GordianClaude ShannonGPS Positioning300350000Presented
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CompanyProductLicenses
0GoogleAnalytics150
1GooglePrediction150
2GoogleTracking300
3BOBOAnalytics150
4IKEAAnalytics300
5Tesla Inc.Analytics300
6Tesla Inc.Prediction150
7MicrosoftTracking300
8WalmartAnalytics150
9AppleAnalytics300
10Exxon MobileAnalytics150
11ATTTracking150
12ATTPrediction150
13CVS HealthTracking450
14SalesforceAnalytics750
15CiscoAnalytics300
16CiscoGPS Positioning300
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ProductAnalyticsGPS PositioningPredictionTracking
Company
Google150.0NaN150.0300.0
ATTNaNNaN150.0150.0
Apple300.0NaNNaNNaN
BOBO150.0NaNNaNNaN
CVS HealthNaNNaNNaN450.0
Cisco300.0300.0NaNNaN
Exxon Mobile150.0NaNNaNNaN
IKEA300.0NaNNaNNaN
MicrosoftNaNNaNNaN300.0
Salesforce750.0NaNNaNNaN
Tesla Inc.300.0NaN150.0NaN
Walmart150.0NaNNaNNaN
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Account NumberCompanyContactAccount ManagerProductLicensesSale PriceStatus
02123398GoogleLarry PagerEdward ThorpAnalytics1502100000Presented
12123398GoogleLarry PagerEdward ThorpPrediction150700000Presented
22123398GoogleLarry PagerEdward ThorpTracking300350000Under Review
32192650BOBOLarry PagerEdward ThorpAnalytics1502450000Lost
4420496IKEAElon TuskEdward ThorpAnalytics3004550000Won
5636685Tesla Inc.Elon TuskEdward ThorpAnalytics3002800000Under Review
6636685Tesla Inc.Elon TuskEdward ThorpPrediction150700000Presented
71216870MicrosoftWill GratesEdward ThorpTracking300350000Under Review
82200450WalmartWill GratesEdward ThorpAnalytics1502450000Lost
9405886AppleCindy PhonerClaude ShannonAnalytics3004550000Won
10470248Exxon MobileCindy PhonerClaude ShannonAnalytics1502100000Presented
11698032ATTCindy PhonerClaude ShannonTracking150350000Under Review
12698032ATTCindy PhonerClaude ShannonPrediction150700000Presented
13902797CVS HealthEmma GordianClaude ShannonTracking450490000Won
142046943SalesforceEmma GordianClaude ShannonAnalytics7507000000Won
152169499CiscoEmma GordianClaude ShannonAnalytics3004550000Lost
162169499CiscoEmma GordianClaude ShannonGPS Positioning300350000Presented
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Account NumberLicensesSale Price
Company
Google63701946003150000
ATT13960643001050000
Apple4058863004550000
BOBO21926501502450000
CVS Health902797450490000
Cisco43389986004900000
Exxon Mobile4702481502100000
IKEA4204963004550000
Microsoft1216870300350000
Salesforce20469437507000000
Tesla Inc.12733704503500000
Walmart22004501502450000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LicensesSale Price
Company
Google6003150000
ATT3001050000
Apple3004550000
BOBO1502450000
CVS Health450490000
Cisco6004900000
Exxon Mobile1502100000
IKEA3004550000
Microsoft300350000
Salesforce7507000000
Tesla Inc.4503500000
Walmart1502450000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LicensesSale Price
Company
Google6003150000
ATT3001050000
Apple3004550000
BOBO1502450000
CVS Health450490000
Cisco6004900000
Exxon Mobile1502100000
IKEA3004550000
Microsoft300350000
Salesforce7507000000
Tesla Inc.4503500000
Walmart1502450000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LicensesSale Price
Company
Google6003150000
ATT3001050000
Apple3004550000
BOBO1502450000
CVS Health450490000
Cisco6004900000
Exxon Mobile1502100000
IKEA3004550000
Microsoft300350000
Salesforce7507000000
Tesla Inc.4503500000
Walmart1502450000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Sale Price
Account ManagerContact
Claude ShannonCindy Phoner7700000
Emma Gordian12390000
Edward ThorpElon Tusk8050000
Larry Pager5600000
Will Grates2800000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sum
Sale Price
ProductAnalyticsGPS PositioningPredictionTracking
Account ManagerContact
Claude ShannonCindy Phoner6650000.0NaN700000.0350000.0
Emma Gordian11550000.0350000.0NaN490000.0
Edward ThorpElon Tusk7350000.0NaN700000.0NaN
Larry Pager4550000.0NaN700000.0350000.0
Will Grates2450000.0NaNNaN350000.0
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sum
Sale Price
ProductAnalyticsGPS PositioningPredictionTracking
Account ManagerContact
Claude ShannonCindy Phoner66500000700000350000
Emma Gordian115500003500000490000
Edward ThorpElon Tusk735000007000000
Larry Pager45500000700000350000
Will Grates245000000350000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
summean
Sale PriceSale Price
ProductAnalyticsGPS PositioningPredictionTrackingAnalyticsGPS PositioningPredictionTracking
Account ManagerContact
Claude ShannonCindy Phoner6650000070000035000033250000700000350000
Emma Gordian11550000350000049000057750003500000490000
Edward ThorpElon Tusk735000007000000367500007000000
Larry Pager4550000070000035000022750000700000350000
Will Grates245000000350000245000000350000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sum
LicensesSale Price
ProductAnalyticsGPS PositioningPredictionTrackingAnalyticsGPS PositioningPredictionTracking
Account ManagerContact
Claude ShannonCindy Phoner450015015066500000700000350000
Emma Gordian10503000450115500003500000490000
Edward ThorpElon Tusk60001500735000007000000
Larry Pager300015030045500000700000350000
Will Grates15000300245000000350000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sum
LicensesSale Price
Account ManagerContactProduct
Claude ShannonCindy PhonerAnalytics4506650000
Prediction150700000
Tracking150350000
Emma GordianAnalytics105011550000
GPS Positioning300350000
Tracking450490000
Edward ThorpElon TuskAnalytics6007350000
Prediction150700000
Larry PagerAnalytics3004550000
Prediction150700000
Tracking300350000
Will GratesAnalytics1502450000
Tracking300350000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sum
LicensesSale Price
Account ManagerContactProduct
Claude ShannonCindy PhonerAnalytics4506650000
Prediction150700000
Tracking150350000
Emma GordianAnalytics105011550000
GPS Positioning300350000
Tracking450490000
Edward ThorpElon TuskAnalytics6007350000
Prediction150700000
Larry PagerAnalytics3004550000
Prediction150700000
Tracking300350000
Will GratesAnalytics1502450000
Tracking300350000
All450036540000
\n", "
" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sum
Sale Price
Account ManagerStatus
Claude ShannonLost4550000
Presented3150000
Under Review350000
Won12040000
Edward ThorpLost4900000
Presented3500000
Under Review3500000
Won4550000
All36540000
\n", "
" ], "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 }