Perspective tries to infer as much information as possible from already-pivoted dataframes:
import pandas as pd
import numpy as np
from perspective import Table, PerspectiveWidget
For the dataset, we'll use superstore.arrow
which is used in various Perspective demos:
import requests
# Download the arrow
url = "https://unpkg.com/@jpmorganchase/perspective-examples@0.2.0-beta.2/build/superstore.arrow"
req = requests.get(url)
arrow = req.content
# Create a table
table = Table(arrow)
view = table.view()
df = view.to_df()
display(df.shape, df.dtypes)
Create a group byed dataframe, and pass it into PerspectiveWidget
:
df_pivoted = df.set_index(['Country', 'Region'])
df_pivoted.head()
# Pivots will be read from the df and applied
PerspectiveWidget(df_pivoted)
The same is true with split by:
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
# Pivots again automatically applied
PerspectiveWidget(df_col)
pt = pd.pivot_table(df, values = 'Discount', index=['Country','Region'], columns = ['Category', 'Segment'])
pt
PerspectiveWidget(pt)
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)}
df2 = pd.DataFrame(arrays)
df2
df2_pivot = df2.pivot_table(values=['D'], index=['A'], columns=['B','C'], aggfunc={'D':'count'})
df2_pivot
PerspectiveWidget(df2_pivot)
pt = pd.pivot_table(df, values = ['Discount','Sales'], index=['Country','Region'],aggfunc={'Discount':'count','Sales':'sum'},columns=["State","Quantity"])
pt
PerspectiveWidget(pt)