import pandas as pd
import numpy as np
from perspective import PerspectiveWidget
# !pip install pylantern
import lantern as l
# basic
df = l.superstore(100)
line = l.line()
df_pivoted = df.set_index(['Country', 'Region'])
df_pivoted.head()
Row ID | Order ID | Order Date | Ship Date | Ship Mode | Customer ID | Segment | City | State | Postal Code | Product ID | Category | Sub-Category | Sales | Quantity | Discount | Profit | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Country | Region | |||||||||||||||||
US | Region 0 | 0 | 63-4964079 | 2020-10-20 | 2020-10-26 | Standard Class | IKO 4183 | A | West Seanton | Virginia | 84971 | OJGL3222720799225 | Consumer Staples | Beverages | 5000 | 350 | 46.44 | 795.91 |
Region 2 | 1 | 57-8494245 | 2020-06-25 | 2020-08-08 | Second Class | V95 7EP | D | Dawsonburgh | Vermont | 56736 | LQYX0062999212822 | Real Estate | Real Estate Management & Development | 1500 | 620 | 35.45 | 415.19 | |
Region 1 | 2 | 03-2779629 | 2020-04-28 | 2020-05-09 | Standard Class | LMD-6068 | C | Lake Chelsealand | Kentucky | 62187 | NSAE8004807331908 | Utilities | Independent Power and Renewable Electricity Pr... | 5300 | 340 | 93.91 | 287.28 | |
Region 2 | 3 | 63-3136985 | 2020-01-30 | 2020-08-03 | Standard Class | 62K DJ8 | D | East Stacey | Wisconsin | 61615 | JKKC9539927675852 | Telecommunication Services | Diversified Telecommunication Services | 9200 | 630 | 94.93 | 401.90 | |
Region 1 | 4 | 64-3554268 | 2020-05-07 | 2020-07-14 | First Class | JPB-6717 | B | Moralesstad | Ohio | 94452 | GNCF5305529751523 | Financials | Capital Markets | 9400 | 40 | 10.33 | 155.71 |
psp = PerspectiveWidget(df_pivoted)
psp
PerspectiveWidget(columns=['index', 'Country', 'Region', 'Row ID', 'Order ID', 'Order Date', 'Ship Date', 'Shi…
arrays = [np.array(['bar', 'bar', 'bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo', 'foo', 'foo', 'foo', 'qux', 'qux', 'qux', 'qux']),
np.array(['one', 'one', 'two', 'two', 'one', 'one', 'two', 'two', 'one', 'one', 'two', 'two', 'one', 'one', 'two', 'two']),
np.array(['X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y'])]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second', 'third',])
df_col = pd.DataFrame(np.random.randn(3, 16), index=['A', 'B', 'C'], columns=index)
df_col
first | bar | baz | foo | qux | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
second | one | two | one | two | one | two | one | two | ||||||||
third | X | Y | X | Y | X | Y | X | Y | X | Y | X | Y | X | Y | X | Y |
A | -0.608338 | -0.801861 | 1.521344 | 0.510841 | 0.195968 | -1.063473 | 0.637491 | -0.018413 | 3.515088 | 0.806254 | 0.904885 | 0.126418 | -0.853604 | 0.557451 | 0.046582 | 0.110949 |
B | 0.831405 | 0.028673 | 0.558204 | -0.328101 | 1.270568 | -0.746025 | -0.043396 | -1.032629 | -0.803371 | -0.599530 | -0.831865 | -0.850051 | -0.934319 | 0.530225 | -1.484300 | 2.060547 |
C | 0.060193 | -0.203357 | -1.139727 | -0.267301 | 0.873535 | 1.342185 | 0.020076 | 2.369631 | 1.333244 | 0.408921 | -1.159719 | -0.348250 | 1.808179 | 1.360236 | -0.772504 | 1.034312 |
psp = PerspectiveWidget(df_col)
psp
PerspectiveWidget(column_pivots=['first', 'second', 'third'], columns=['value'], row_pivots=['index'])
pt = pd.pivot_table(df, values = 'Discount', index=['Country','Region'], columns = ['Category', 'Segment'])
pt
Category | Consumer Discretionary | Consumer Staples | Energy | ... | Real Estate | Telecommunication Services | Utilities | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Segment | A | B | C | D | A | B | C | D | A | C | ... | B | C | D | A | B | C | D | A | C | D | |
Country | Region | |||||||||||||||||||||
US | Region 0 | NaN | 73.75 | NaN | 11.50 | 46.44 | 54.96 | 75.46 | 11.34 | 55.09 | 40.520 | ... | NaN | NaN | 31.73 | NaN | NaN | NaN | NaN | NaN | 89.19 | 51.46 |
Region 1 | NaN | NaN | NaN | NaN | NaN | 50.62 | NaN | NaN | NaN | NaN | ... | NaN | NaN | 6.91 | NaN | NaN | NaN | NaN | NaN | 93.91 | NaN | |
Region 2 | NaN | 60.37 | 44.22 | 91.56 | 21.42 | NaN | NaN | 57.69 | NaN | NaN | ... | NaN | 81.07 | 35.45 | NaN | 82.74 | 51.9 | 94.93 | 22.45 | NaN | NaN | |
Region 3 | NaN | 30.58 | NaN | NaN | NaN | NaN | NaN | 94.13 | NaN | 30.315 | ... | NaN | NaN | NaN | NaN | 57.14 | NaN | NaN | NaN | NaN | NaN | |
Region 4 | 15.58 | 12.09 | NaN | 91.30 | NaN | NaN | NaN | 43.20 | 55.37 | NaN | ... | 98.22 | 72.39 | 78.22 | 30.85 | 8.26 | NaN | 77.91 | NaN | NaN | 61.92 |
5 rows × 40 columns
psp = PerspectiveWidget(pt)
psp
PerspectiveWidget(column_pivots=['Category', 'Segment'], columns=['value'], row_pivots=['Country', 'Region'])
arrays = {'A':['bar', 'bar', 'bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo', 'foo', 'foo', 'foo', 'qux', 'qux', 'qux', 'qux'],
'B':['one', 'one', 'two', 'two', 'one', 'one', 'two', 'two', 'one', 'one', 'two', 'two', 'one', 'one', 'two', 'two'],
'C':['X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y'],
'D':np.arange(16)}
df = pd.DataFrame(arrays)
df
A | B | C | D | |
---|---|---|---|---|
0 | bar | one | X | 0 |
1 | bar | one | Y | 1 |
2 | bar | two | X | 2 |
3 | bar | two | Y | 3 |
4 | baz | one | X | 4 |
5 | baz | one | Y | 5 |
6 | baz | two | X | 6 |
7 | baz | two | Y | 7 |
8 | foo | one | X | 8 |
9 | foo | one | Y | 9 |
10 | foo | two | X | 10 |
11 | foo | two | Y | 11 |
12 | qux | one | X | 12 |
13 | qux | one | Y | 13 |
14 | qux | two | X | 14 |
15 | qux | two | Y | 15 |
df_pivot=df.pivot_table(values=['D'], index=['A'], columns=['B','C'], aggfunc={'D':'count'})
df_pivot
D | ||||
---|---|---|---|---|
B | one | two | ||
C | X | Y | X | Y |
A | ||||
bar | 1 | 1 | 1 | 1 |
baz | 1 | 1 | 1 | 1 |
foo | 1 | 1 | 1 | 1 |
qux | 1 | 1 | 1 | 1 |
psp = PerspectiveWidget(df_pivot)
psp
PerspectiveWidget(column_pivots=['B', 'C'], columns=['value'], row_pivots=['A'])
df = l.superstore(100)
pt = pd.pivot_table(df, values = ['Discount','Sales'], index=['Country','Region'],aggfunc={'Discount':'count','Sales':'sum'},columns=["State","Quantity"])
pt
Discount | ... | Sales | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
State | Alabama | Alaska | Arizona | California | ... | Utah | Vermont | Virginia | Washington | West Virginia | Wisconsin | Wyoming | ||||||||||
Quantity | 90 | 490 | 130 | 120 | 190 | 290 | 510 | 680 | 600 | 770 | ... | 750 | 890 | 170 | 330 | 840 | 310 | 900 | 780 | 910 | 710 | |
Country | Region | |||||||||||||||||||||
US | Region 0 | 1.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | ... | 600.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
Region 1 | NaN | 1.0 | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | NaN | ... | NaN | 2000.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | |
Region 2 | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | 1.0 | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | |
Region 3 | NaN | NaN | 1.0 | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | ... | NaN | NaN | NaN | NaN | 9100.0 | 1700.0 | 10000.0 | 4300.0 | NaN | 4800.0 | |
Region 4 | NaN | NaN | NaN | 1.0 | 1.0 | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | 500.0 | 7800.0 | NaN | NaN | NaN | NaN | 4700.0 | NaN |
5 rows × 200 columns
psp = PerspectiveWidget(pt)
psp
PerspectiveWidget(column_pivots=['State', 'Quantity'], columns=['Discount', 'Sales'], row_pivots=['Country', '…