Drag'n'Drop Pivot Tables and Charts, in Jupyter/IPython Notebook

PivotTable.js is a Javascript Pivot Table and Pivot Chart library with drag’n’drop interactivity, and it can now be used with Jupyter/IPython Notebook via the pivottablejs module. This has been possible for RStudio users for a while now via rPivotTable, but why should they have all the fun?

I first built PivotTable.js with a plan to build an in-browser data analysis tool, and got as far as one where you could load up a CSV file in the browser for display. Since then, however, the Jupyter project has gathered steam and now provides a browser-based interface to some of the most powerful data processing libraries in the world, so it makes sense to interface with it.

The fantastic Pandas library for Python already has a pivot_table method, which is quite powerful, but exploring data by executing, modifying, executing, modifying code is nowhere as fast as just dragging elements around a UI and seeing patterns appear interactively, and this is what using PivotTable.js gives you. Check out the video above for a taste, or check out my analysis of Montreal 311 Service Requests data for a more complete example with voiceover.

Installation and Use

To use PivotTable.js from Jupyter, first install it with pip install pivottablejs.

Then, if you have a Pandas DataFrame (from Pandas v0.14+, or any other object with a to_csv method which returns a string) called df just execute

from pivottablejs import pivot_ui
pivot_ui(df)

and you will get an interactive UI in your notebook.

What happens behind the scenes is that a local file called pivottablejs.html is written (overrideable behaviour with the outfile_path keyword arg), which contains your data in CSV form and some HTML/Javascript to load up the UI, which Jupyter then loads up in an iframe. You can also “pop out” of that frame into a full page, which is then saveable for later.

The pivottablejs Python module is available from PyPi under the same free MIT license as PivotTable.js. The source is up on Github and I’d love feedback, pull requests etc.


© Nicolas Kruchten 2010-2024