%load_ext autoreload
%autoreload 2
import pandas as pd
import ipypivot as pt
ipypivot
¶.Pivot()
corresponds to pivotTable.js pivot()
method.PivotUI_Box()
corresponds to pivotTable.js pivotUI()
methodPivot_Options
or PivotUI_Options
object.PivotUI
object p (say p
for example) has 2 buttons: Save and Restore.p.table.df_export
and dict p.table.options
.option
object is changed from the Python side, all views will be automatically re-rerendered.df = pt.samples.df_tips
dic = {
'sumOverSum': '$.pivotUtilities.aggregators["Sum over Sum"]',
'heatmap': '$.pivotUtilities.renderers["Heatmap"]'
}
p = pt.Pivot(df_data=df)
opts = p.options
opts.rows = ['sex', 'smoker']
opts.cols = ['day', 'time']
opts.vals = ['tip', 'total_bill']
opts.aggregator = '{sumOverSum}(["tip", "total_bill"])'.format(**dic)
opts.renderer = '{heatmap}'.format(**dic)
p
Failed to display Jupyter Widget of type Pivot
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
df = pt.samples.df_iris
p = pt.Pivot(df_data=df)
opts = p.options
opts.rows = ['Petal.Length']
opts.cols = ['Petal.Width', 'Species']
opts.renderer = '$.pivotUtilities.c3_renderers["Scatter Chart"]'
opts.rendererOptions = {
'c3': {
'size': {
'width': 700, 'height': 500
}
}
}
p
Failed to display Jupyter Widget of type Pivot
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
PivotUI_Options
helper objectdf = pt.samples.df_tips
p = pt.PivotUI(df_data=df)
opts = p.table.options
dic = {
'sumOverSum': '$.pivotUtilities.aggregators["Sum over Sum"]',
'heatmap': '$.pivotUtilities.renderers["Heatmap""]'
}
opts.rows = ['sex', 'smoker']
opts.cols = ['day', 'time']
opts.vals = ['tip', 'total_bill']
opts.aggregatorName = 'Sum over Sum'
opts.rendererName = 'Heatmap'
p
Failed to display Jupyter Widget of type PivotUIBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
df = pt.samples.df_mps
p = pt.PivotUI(df_data=df)
opts = p.table.options
opts.rows = ['Gender Imbalance']
opts.cols = ['Age Bin']
opts.derivedAttributes = {
'Age Bin': '$.pivotUtilities.derivers.bin("Age", 10)',
'Gender Imbalance': 'function(mp) { return mp["Gender"] == "Male" ? 1 : -1; }'
}
p
Failed to display Jupyter Widget of type PivotUIBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
df = pt.samples.df_mps
p = pt.PivotUI(df_data=df)
opts = p.table.options
opts.rows = ['Province']
opts.cols = ['Party']
opts.aggregatorName = 'Integer Sum'
opts.vals = ['Age']
opts.rendererName = 'Heatmap'
opts.rendererOptions = {
'table': {
'clickCallback': """function(e, value, filters, pivotData){
var names = [];
pivotData.forEachMatchingRecord(filters,
function(record){ names.push(record.Name); });
alert(names.join(\"\\n\"));
}"""
}
}
print('-> Click on a cell to view the contents')
p
-> Click on a cell to view the contents
Failed to display Jupyter Widget of type PivotUIBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
df = pt.samples.df_mps
p = pt.PivotUI(df_data=df)
opts = p.table.options
dic = {
'tpl': '$.pivotUtilities.aggregatorTemplates',
'sortAs': '$.pivotUtilities.sortAs'
}
opts.rows = ['Province']
opts.cols = ['Party']
opts.aggregators = {
"Number of MPs": 'function() {{ return {tpl}.count()() }}'.format(**dic),
"Average Age of MPs": 'function() {{ return {tpl}.average()(["Age"]) }}'.format(**dic)
}
opts.sorters = {
'Age': 'function(a,b){ return b-a; }', # sort backwards
'Province': """{sortAs}(["British Columbia", "Alberta", "Saskatchewan", "Manitoba",
"Territories", "Ontario", "Quebec", "New Brunswick",
"Prince Edward Island", "Nova Scotia",
"Newfoundland and Labrador"])""".format(**dic)
}
p
Failed to display Jupyter Widget of type PivotUIBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
df = pt.samples.df_mps
p = pt.PivotUI(df_data=df)
opts = p.table.options
opts.rows = ['Province']
opts.cols = ['Party']
opts.renderers = '$.extend($.pivotUtilities.renderers, $.pivotUtilities.c3_renderers)'
opts.rendererName = 'Horizontal Stacked Bar Chart'
opts.rowOrder = 'value_z_to_a'
opts.colOrder = 'value_z_to_a'
opts.rendererOptions = {
'c3': {
'data': {
'colors': {
'Liberal': '#dc3912',
'Conservative': '#3366cc',
'NDP': '#ff9900',
'Green':'#109618',
'Bloc Quebecois': '#990099'
}
}
}
}
p
Failed to display Jupyter Widget of type PivotUIBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
df = pt.samples.df_mps
p = pt.PivotUI(df_data=df)
opts = p.table.options
opts.rows = ['Province', 'Party']
opts.cols = []
opts.rendererName = 'Treemap'
p
Failed to display Jupyter Widget of type PivotUIBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
df = pt.samples.df_weather
p = pt.PivotUI(df_data=df)
opts = p.table.options
putil = '$.pivotUtilities'
dic = {
'dateFormat': putil+'.derivers.dateFormat',
'sortAs': putil+'.sortAs',
'tpl': putil+'.aggregatorTemplates',
'numberFormat': putil+".numberFormat({ suffix: ' °C' })"
}
dic['putil'] = putil
opts.hiddenAttributes = ['Date', 'Max Temp (C)', 'Mean Temp (C)',
'Min Temp (C)', 'Total Rain (mm)', 'Total Snow (cm)']
opts.derivedAttributes = {
'month name': "{dateFormat}('Date', '%n', true)".format(**dic),
'day name': "{dateFormat}('Date', '%w', true)".format(**dic)
}
opts.rows = ['day name']
opts.cols = ['month name']
opts.sorters = {
'month name': """{sortAs}(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])""".format(**dic),
'day name': "{sortAs}(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])".format(**dic)
}
opts.aggregators = {
'Mean Temperature': "function () {{ return {tpl}.average({numberFormat})(['Mean Temp (C)']) }}".format(**dic),
'Max Temperature': "function () {{ return {tpl}.max({numberFormat})(['Max Temp (C)']) }}".format(**dic),
'Min Temperature': "function () {{ return {tpl}.min({numberFormat})(['Min Temp (C)']) }}".format(**dic)
}
opts.renderers = '$.extend({putil}.renderers, {putil}.c3_renderers, {putil}.export_renderers)'.format(**dic)
opts.rendererName = 'Heatmap'
opts.rendererOptions = {
'heatmap': {
'colorScaleGenerator': """function (values) {
return d3.scale.linear()
.domain([-35, 0, 35])
.range(['#77F', '#FFF', '#F77'])
}"""
}
}
p
Failed to display Jupyter Widget of type PivotUIBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
# p.table.df_export
p.table.options.aggregatorName = 'Max Temperature'
p.table.options.aggregators = {
'Mean Temperature': "function () {{ return {tpl}.average({numberFormat})(['Mean Temp (C)']) }}".format(**dic),
'Max Temperature': "function () {{ return {tpl}.max({numberFormat})(['Max Temp (C)']) }}".format(**dic)
}
p.table.options.rendererOptions = {
'heatmap': {
'colorScaleGenerator': """function (values) {
return d3.scale.linear()
.domain([-20, 0, 20])
.range(['#77F', '#FFF', '#F77'])
}"""
}
}
p.table.options.to_dict()
{'aggregatorName': 'Max Temperature', 'aggregators': {'Max Temperature': "function () { return $.pivotUtilities.aggregatorTemplates.max($.pivotUtilities.numberFormat({ suffix: ' °C' }))(['Max Temp (C)']) }", 'Mean Temperature': "function () { return $.pivotUtilities.aggregatorTemplates.average($.pivotUtilities.numberFormat({ suffix: ' °C' }))(['Mean Temp (C)']) }"}, 'cols': ['month name'], 'derivedAttributes': {'day name': "$.pivotUtilities.derivers.dateFormat('Date', '%w', true)", 'month name': "$.pivotUtilities.derivers.dateFormat('Date', '%n', true)"}, 'hiddenAttributes': ['Date', 'Max Temp (C)', 'Mean Temp (C)', 'Min Temp (C)', 'Total Rain (mm)', 'Total Snow (cm)'], 'rendererName': 'Heatmap', 'rendererOptions': {'heatmap': {'colorScaleGenerator': "function (values) {\n return d3.scale.linear()\n .domain([-20, 0, 20])\n .range(['#77F', '#FFF', '#F77'])\n }"}}, 'renderers': '$.extend($.pivotUtilities.renderers, $.pivotUtilities.c3_renderers, $.pivotUtilities.export_renderers)', 'rows': ['day name'], 'sorters': {'day name': "$.pivotUtilities.sortAs(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])", 'month name': "$.pivotUtilities.sortAs(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \n 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])"}}