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.
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
iframe. You can also “pop out” of that frame into a full page, which is then saveable for later.
Code from this post is up on Github at nicolaskruchten/jupyter_pivottablejs