├── Pandas MultiIndex Tutorial.ipynb ├── README.md ├── data.csv ├── mocker.py └── upc_meta_data.csv /README.md: -------------------------------------------------------------------------------- 1 | # Pandas MultiIndex Tutorial 2 | 3 | An in-depth introduction to Pandas' MultiIndexes including practical code snippets. 4 | 5 | The tutorial includes realistic data as would be tracked by a grocery retailer, with the hopes that it will aid in understanding versus the more commonly seen randomly generated data. Also included is a short script (mocker.py) to create additional mock data like that provided in data.csv. 6 | -------------------------------------------------------------------------------- /data.csv: -------------------------------------------------------------------------------- 1 | Date,Store,Category,Subcategory,UPC EAN,Description,Dollars,Units 2 | 7/11/18,Store 3,Beer,Ales,0736920111112,Goose Island - Honkers Ale - 6 Pack,35.68,4 3 | 7/11/18,Store 2,Beer,Ales,0736920111112,Goose Island - Honkers Ale - 6 Pack,166.74,14 4 | 7/10/18,Store 3,Beer,Ales,0736920111112,Goose Island - Honkers Ale - 6 Pack,67.36,8 5 | 7/10/18,Store 2,Beer,Ales,0736920111112,Goose Island - Honkers Ale - 6 Pack,80.01,9 6 | 7/12/18,Store 3,Beer,Ales,0736920111112,Goose Island - Honkers Ale - 6 Pack,78.3,10 7 | 7/12/18,Store 2,Beer,Ales,0736920111112,Goose Island - Honkers Ale - 6 Pack,37.75,5 8 | 7/14/18,Store 3,Beer,Ales,0736920111112,Goose Island - Honkers Ale - 6 Pack,113.49000000000001,13 9 | 7/14/18,Store 2,Beer,Ales,0736920111112,Goose Island - Honkers Ale - 6 Pack,45.05,5 10 | 7/10/18,Store 2,Beer,Ales,0094922755711,Bowser Beer - Beefy Brown Ale - 6 Pack,81.76,8 11 | 7/10/18,Store 3,Beer,Ales,0094922755711,Bowser Beer - Beefy Brown Ale - 6 Pack,62.48,8 12 | 7/11/18,Store 2,Beer,Ales,0094922755711,Bowser Beer - Beefy Brown Ale - 6 Pack,104.91,13 13 | 7/11/18,Store 3,Beer,Ales,0094922755711,Bowser Beer - Beefy Brown Ale - 6 Pack,72.72,9 14 | 7/16/18,Store 2,Beer,Ales,0094922755711,Bowser Beer - Beefy Brown Ale - 6 Pack,99.39999999999999,10 15 | 7/16/18,Store 3,Beer,Ales,0094922755711,Bowser Beer - Beefy Brown Ale - 6 Pack,46.65,5 16 | 7/14/18,Store 2,Beer,Ales,0094922755711,Bowser Beer - Beefy Brown Ale - 6 Pack,124.52000000000001,11 17 | 7/14/18,Store 3,Beer,Ales,0094922755711,Bowser Beer - Beefy Brown Ale - 6 Pack,113.67999999999999,14 18 | 7/15/18,Store 2,Beer,Ales,0094922755711,Bowser Beer - Beefy Brown Ale - 6 Pack,88.77000000000001,11 19 | 7/15/18,Store 3,Beer,Ales,0094922755711,Bowser Beer - Beefy Brown Ale - 6 Pack,119.47,13 20 | 7/12/18,Store 3,Beer,Lagers,0702770081011,Omission - Gluten Free Lager - 6 Pack,93.10000000000001,10 21 | 7/12/18,Store 1,Beer,Lagers,0702770081011,Omission - Gluten Free Lager - 6 Pack,40.12,4 22 | 7/14/18,Store 3,Beer,Lagers,0702770081011,Omission - Gluten Free Lager - 6 Pack,128.64000000000001,12 23 | 7/14/18,Store 1,Beer,Lagers,0702770081011,Omission - Gluten Free Lager - 6 Pack,85.04,8 24 | 7/10/18,Store 3,Beer,Lagers,0702770081011,Omission - Gluten Free Lager - 6 Pack,176.96,14 25 | 7/10/18,Store 1,Beer,Lagers,0702770081011,Omission - Gluten Free Lager - 6 Pack,137.67,13 26 | 7/16/18,Store 3,Beer,Lagers,0702770081011,Omission - Gluten Free Lager - 6 Pack,104.86,7 27 | 7/16/18,Store 1,Beer,Lagers,0702770081011,Omission - Gluten Free Lager - 6 Pack,174.72,14 28 | 7/12/18,Store 3,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,109.33999999999999,14 29 | 7/12/18,Store 2,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,116.71,11 30 | 7/14/18,Store 3,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,31.52,4 31 | 7/14/18,Store 2,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,96.8,10 32 | 7/13/18,Store 3,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,75.74000000000001,7 33 | 7/13/18,Store 2,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,82.67,7 34 | 7/10/18,Store 3,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,119.9,10 35 | 7/10/18,Store 2,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,78.39999999999999,7 36 | 7/11/18,Store 3,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,143.51999999999998,13 37 | 7/11/18,Store 2,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,56.14,7 38 | 7/15/18,Store 3,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,111.47999999999999,12 39 | 7/15/18,Store 2,Beer,Stouts,0083783575217,Sierra Nevada Brewing Co. - Stout - 6 Pack,134.52,12 40 | 7/10/18,Store 2,Beer,Ales,0702770082018,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,89.76,6 41 | 7/10/18,Store 3,Beer,Ales,0702770082018,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,135.72,13 42 | 7/11/18,Store 2,Beer,Ales,0702770082018,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,156.24,12 43 | 7/11/18,Store 3,Beer,Ales,0702770082018,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,184.99,13 44 | 7/16/18,Store 2,Beer,Ales,0702770082018,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,135.94,14 45 | 7/16/18,Store 3,Beer,Ales,0702770082018,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,143.6,10 46 | 7/15/18,Store 2,Beer,Ales,0702770082018,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,160.2,12 47 | 7/15/18,Store 3,Beer,Ales,0702770082018,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,66.7,5 48 | 7/12/18,Store 2,Beer,Ales,0702770082018,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,109.2,8 49 | 7/12/18,Store 3,Beer,Ales,0702770082018,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,56.760000000000005,6 50 | 7/16/18,Store 2,Beer,Malts,0071250000140,Colt 45 - Malt Liquor - 6 Pack,47.55,5 51 | 7/16/18,Store 3,Beer,Malts,0071250000140,Colt 45 - Malt Liquor - 6 Pack,108.47999999999999,12 52 | 7/11/18,Store 2,Beer,Malts,0071250000140,Colt 45 - Malt Liquor - 6 Pack,101.79,9 53 | 7/11/18,Store 3,Beer,Malts,0071250000140,Colt 45 - Malt Liquor - 6 Pack,107.58,11 54 | 7/15/18,Store 2,Beer,Malts,0071250000140,Colt 45 - Malt Liquor - 6 Pack,139.23000000000002,13 55 | 7/15/18,Store 3,Beer,Malts,0071250000140,Colt 45 - Malt Liquor - 6 Pack,40.5,5 56 | 7/13/18,Store 2,Beer,Malts,0071250000140,Colt 45 - Malt Liquor - 6 Pack,103.59,9 57 | 7/13/18,Store 3,Beer,Malts,0071250000140,Colt 45 - Malt Liquor - 6 Pack,142.44,12 58 | 7/12/18,Store 1,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,60.24,8 59 | 7/12/18,Store 3,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,69.29,13 60 | 7/16/18,Store 1,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,126.14,14 61 | 7/16/18,Store 3,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,66.16,8 62 | 7/15/18,Store 1,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,115.68,12 63 | 7/15/18,Store 3,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,132.6,13 64 | 7/10/18,Store 1,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,56.88,8 65 | 7/10/18,Store 3,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,37.8,6 66 | 7/11/18,Store 1,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,50.400000000000006,6 67 | 7/11/18,Store 3,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,47.25,7 68 | 7/14/18,Store 1,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,41.23,7 69 | 7/14/18,Store 3,Wine,Red,0089744402585,Trapiche - Malbec - 750 mL Bottle,20.150000000000002,5 70 | 7/14/18,Store 1,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,32.099999999999994,6 71 | 7/14/18,Store 3,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,38.0,5 72 | 7/13/18,Store 1,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,38.6,5 73 | 7/13/18,Store 3,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,17.64,4 74 | 7/10/18,Store 1,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,46.3,10 75 | 7/10/18,Store 3,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,34.56,6 76 | 7/16/18,Store 1,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,68.46,14 77 | 7/16/18,Store 3,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,46.56,6 78 | 7/15/18,Store 1,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,54.27,9 79 | 7/15/18,Store 3,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,58.199999999999996,12 80 | 7/12/18,Store 1,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,95.16,12 81 | 7/12/18,Store 3,Wine,Red,0492130008399,Yellow Tail - Merlot - 750 mL Bottle,75.6,10 82 | 7/13/18,Store 3,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,287.28,14 83 | 7/13/18,Store 2,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,121.32,6 84 | 7/12/18,Store 3,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,85.6,4 85 | 7/12/18,Store 2,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,196.74,9 86 | 7/10/18,Store 3,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,278.59999999999997,14 87 | 7/10/18,Store 2,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,293.16,14 88 | 7/11/18,Store 3,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,247.51999999999998,13 89 | 7/11/18,Store 2,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,237.16,11 90 | 7/14/18,Store 3,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,80.96,4 91 | 7/14/18,Store 2,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,281.96000000000004,14 92 | 7/16/18,Store 3,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,266.84,14 93 | 7/16/18,Store 2,Wine,White,0081248501095,Curious Beasts - Chardonnay - 750 mL Bottle,82.8,4 94 | 7/10/18,Store 1,Wine,Rose,0085000024218,Dark Horse - S. Blanc Rose - 750 mL Bottle,148.60999999999999,11 95 | 7/10/18,Store 2,Wine,Rose,0085000024218,Dark Horse - S. Blanc Rose - 750 mL Bottle,100.38,7 96 | 7/13/18,Store 1,Wine,Rose,0085000024218,Dark Horse - S. Blanc Rose - 750 mL Bottle,131.6,10 97 | 7/13/18,Store 2,Wine,Rose,0085000024218,Dark Horse - S. Blanc Rose - 750 mL Bottle,156.53,11 98 | 7/12/18,Store 1,Wine,Rose,0085000024218,Dark Horse - S. Blanc Rose - 750 mL Bottle,93.94,7 99 | 7/12/18,Store 2,Wine,Rose,0085000024218,Dark Horse - S. Blanc Rose - 750 mL Bottle,83.82000000000001,6 100 | 7/11/18,Store 1,Wine,Rose,0085000024218,Dark Horse - S. Blanc Rose - 750 mL Bottle,52.24,4 101 | 7/11/18,Store 2,Wine,Rose,0085000024218,Dark Horse - S. Blanc Rose - 750 mL Bottle,173.03,13 102 | 7/10/18,Store 2,Alcohol,Liqour,0080480280024,Grey Goose - Imported Vodka - 750 mL Bottle,231.29999999999998,9 103 | 7/10/18,Store 3,Alcohol,Liqour,0080480280024,Grey Goose - Imported Vodka - 750 mL Bottle,128.3,5 104 | 7/15/18,Store 2,Alcohol,Liqour,0080480280024,Grey Goose - Imported Vodka - 750 mL Bottle,121.19999999999999,5 105 | 7/15/18,Store 3,Alcohol,Liqour,0080480280024,Grey Goose - Imported Vodka - 750 mL Bottle,278.04,12 106 | 7/12/18,Store 2,Alcohol,Liqour,0080480280024,Grey Goose - Imported Vodka - 750 mL Bottle,279.36,12 107 | 7/12/18,Store 3,Alcohol,Liqour,0080480280024,Grey Goose - Imported Vodka - 750 mL Bottle,228.08,8 108 | 7/11/18,Store 2,Alcohol,Liqour,0080480280024,Grey Goose - Imported Vodka - 750 mL Bottle,248.79999999999998,10 109 | 7/11/18,Store 3,Alcohol,Liqour,0080480280024,Grey Goose - Imported Vodka - 750 mL Bottle,296.01,11 110 | 7/16/18,Store 2,Alcohol,Liqour,0080480280024,Grey Goose - Imported Vodka - 750 mL Bottle,221.04,9 111 | 7/16/18,Store 3,Alcohol,Liqour,0080480280024,Grey Goose - Imported Vodka - 750 mL Bottle,126.66,6 112 | 7/12/18,Store 3,Alcohol,Liquor,0674545000001,Don Julio - Tequila Blanco - 750 mL Bottle,268.56,9 113 | 7/12/18,Store 2,Alcohol,Liquor,0674545000001,Don Julio - Tequila Blanco - 750 mL Bottle,410.88,12 114 | 7/16/18,Store 3,Alcohol,Liquor,0674545000001,Don Julio - Tequila Blanco - 750 mL Bottle,345.07,11 115 | 7/16/18,Store 2,Alcohol,Liquor,0674545000001,Don Julio - Tequila Blanco - 750 mL Bottle,161.14999999999998,5 116 | 7/10/18,Store 3,Alcohol,Liquor,0674545000001,Don Julio - Tequila Blanco - 750 mL Bottle,204.12,7 117 | 7/10/18,Store 2,Alcohol,Liquor,0674545000001,Don Julio - Tequila Blanco - 750 mL Bottle,168.06,6 118 | 7/11/18,Store 3,Alcohol,Liquor,0674545000001,Don Julio - Tequila Blanco - 750 mL Bottle,102.04,4 119 | 7/11/18,Store 2,Alcohol,Liquor,0674545000001,Don Julio - Tequila Blanco - 750 mL Bottle,338.1,10 120 | 7/15/18,Store 3,Alcohol,Liquor,0674545000001,Don Julio - Tequila Blanco - 750 mL Bottle,430.5,14 121 | 7/15/18,Store 2,Alcohol,Liquor,0674545000001,Don Julio - Tequila Blanco - 750 mL Bottle,177.0,6 122 | 7/12/18,Store 2,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,250.25,7 123 | 7/12/18,Store 3,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,455.13,13 124 | 7/13/18,Store 2,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,432.74,11 125 | 7/13/18,Store 3,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,185.75,5 126 | 7/11/18,Store 2,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,201.71999999999997,6 127 | 7/11/18,Store 3,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,318.8,8 128 | 7/14/18,Store 2,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,227.82,6 129 | 7/14/18,Store 3,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,268.52,7 130 | 7/15/18,Store 2,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,415.68,12 131 | 7/15/18,Store 3,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,476.64,12 132 | 7/10/18,Store 2,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,172.0,5 133 | 7/10/18,Store 3,Alcohol,Liquor,0080432400630,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,278.8,8 134 | -------------------------------------------------------------------------------- /mocker.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | 3 | import numpy as np 4 | import pandas as pd 5 | 6 | 7 | def mock(upc): 8 | rand_dates = np.random.choice(dates, np.random.randint(4, len(dates)), replace=False) 9 | rand_stores = np.random.choice(stores, np.random.randint(2, len(stores)), replace=False) 10 | 11 | return list(itertools.product(*[rand_dates, rand_stores, [upc]])) 12 | 13 | 14 | if __name__ == '__main__': 15 | dates = ['7/10/18', '7/11/18', '7/12/18', '7/13/18', '7/14/18', '7/15/18', '7/16/18'] 16 | stores = ['Store 1', 'Store 2', 'Store 3'] 17 | upc_meta_data = pd.read_csv('upc_meta_data.csv', dtype={'UPC EAN': str}) 18 | 19 | to_concat = [] 20 | for upc in upc_meta_data['UPC EAN']: 21 | tdf = pd.DataFrame(mock(upc), columns=['Date', 'Store', 'UPC EAN']) 22 | tdf = tdf.merge(upc_meta_data[upc_meta_data['UPC EAN'] == upc], how='left', on='UPC EAN') 23 | tdf['Units'] = np.random.randint(4, 15, tdf.shape[0]) 24 | tdf['Dollars'] = (tdf['Units'] 25 | * np.round(np.random.uniform(tdf['Price Low'], tdf['Price High'], tdf.shape[0]), 2)) 26 | tdf.drop(columns=['Price Low', 'Price High'], inplace=True) 27 | 28 | to_concat.append(tdf) 29 | 30 | df = pd.concat(to_concat) 31 | df = df[['Date', 'Store', 'Category', 'Subcategory', 'UPC EAN', 'Description', 'Dollars', 'Units']] 32 | df.to_csv('data.csv', index=None) 33 | -------------------------------------------------------------------------------- /upc_meta_data.csv: -------------------------------------------------------------------------------- 1 | UPC EAN,Category,Subcategory,Description,Price Low,Price High 2 | 0736920111112,Beer,Ales,Goose Island - Honkers Ale - 6 Pack,7.49,11.99 3 | 0094922755711,Beer,Ales,Bowser Beer - Beefy Brown Ale - 6 Pack,7.49,11.99 4 | 0702770081011,Beer,Lagers,Omission - Gluten Free Lager - 6 Pack,8.99,14.99 5 | 0083783575217,Beer,Stouts,Sierra Nevada Brewing Co. - Stout - 6 Pack,7.49,11.99 6 | 0702770082018,Beer,Ales,Omission Pale Ale - Gluten Free Pale Ale - 6 Pack,8.99,14.99 7 | 0071250000140,Beer,Malts,Colt 45 - Malt Liquor - 6 Pack,7.49,11.99 8 | 0089744402585,Wine,Red,Trapiche - Malbec - 750 mL Bottle,3.49,11.99 9 | 0492130008399,Wine,Red,Yellow Tail - Merlot - 750 mL Bottle,3.99,7.99 10 | 0081248501095,Wine,White,Curious Beasts - Chardonnay - 750 mL Bottle,18.99,21.99 11 | 0085000024218,Wine,Rose,Dark Horse - S. Blanc Rose - 750 mL Bottle,12.99,14.99 12 | 0080480280024,Alcohol,Liqour,Grey Goose - Imported Vodka - 750 mL Bottle,19.99,29.95 13 | 0674545000001,Alcohol,Liquor,Don Julio - Tequila Blanco - 750 mL Bottle,24.99,34.99 14 | 0080432400630,Alcohol,Liquor,Glenlivet - 12 Year Scotch Whisky - 750 mL Bottle,32.99,39.99 --------------------------------------------------------------------------------