Ipython Select Widget- more than one selection

ipython, jupyter-notebook, pandas, python

Solution

You can use the 'SelectMultiple' Widget (IPython.html.widgets.SelectMultiple). It looks like this was introduced since you posted the questions (Bear in mind that the IPython HTML widgets are still subject to API breaking changes).

This will give you the same list as 'SelectWidget' but allow for multiple selection by the user. All selected options will be sent back to your interact call back as a tuple. Also, since you posted this 'values' has changed to 'options'.

The code below should do what you want.

i = interact( my_pivot,
             rows    = SelectMultiple(options=list(df.columns)), 
             values  = SelectMultiple(options=['Profit', 'Stock']),
             aggfunc = SelectMultiple(options={ 'sum' : numpy.sum, 'ave' : numpy.average }))

Finally, you need to update the 'my_pivot' function to make sure it properly handles the arguments it now receives as tuples.

Problem

Copying the example from: http://tooblippe.github.io/insightstack-blog/2014/04/24/pandas-pivot/ How would one configure the widget select boxes to allow for multiple selections at the same time? ``` %pylab inline from pandas import Series, DataFrame, pivot_table import numpy as np import numpy from IPython.html.widgets import interact, SelectWidget, CheckboxWidget, RadioButtonsWidget from IPython.display import display d = { 'Class' : Series( ['a', 'b', 'b','a','a', 'b', 'b','a','a', 'b', 'b','a','a','b','b','b']), 'Area' : Series( ['North','East', 'South', 'West','North','East', 'South', 'West','North','East', 'South', 'West','South', 'West','South', 'West']), 'Type' : Series( ['square', 'round','square', 'round', 'round', 'square', 'round', 'square', 'round', 'square','round', 'square',]), 'Web' : Series( ['Y','N','N','Y','Y','N','N','Y','Y','N','N','Y','Y','N','N','Y']), 'Agent' : Series( ['Mike', 'John', 'Pete','Mike', 'John', 'Pete','Mike', 'John', 'Pete','Mike', 'John', 'Pete','John', 'Pete','John', 'Pete']), 'Income' : Series( [20., 40., 90., 20.]), 'Profit' : Series( [1., 2., 3., 4.,1., 2., 3., 4.,1., 2., 3., 4.,1., 2., 3., 4.]), 'Stock' : Series( [20., 23., 33., 43.,12., 21., 310., 41.,11., 21., 31., 41.,11., 22., 34., 54.] ) } df = DataFrame(d) def my_pivot( rows, values, aggfunc): dfp = df piv = pivot_table( dfp, rows=rows, values=values, aggfunc=aggfunc) print piv i = interact( my_pivot, rows = SelectWidget(values=list(df.columns)), values = SelectWidget(values=['Profit', 'Stock']), aggfunc = SelectWidget( values={ 'sum' : numpy.sum, 'ave' : numpy.average })) ```

Original source