How to move my pandas dataframe to d3?

Anton picture Anton · May 14, 2014 · Viewed 12.5k times · Source

I am new to Python and have worked my way through a few books on it. Everything is great, except visualizations. I really dislike matplotlib and Bokeh requires too heavy of a stack.

The workflow I want is:

Data munging analysis using pandas in ipython notebook -> visualization using d3 in sublimetext2

However, being new to both Python and d3, I don't know the best way to export my pandas dataframe to d3. Should I just have it as a csv? JSON? Or is there a more direct way?

Side question: Is there any (reasonable) way to do everything in an ipython notebook instead of switching to sublimetext?

Any help would be appreciated.

Answer

Guillaume Jacquenot picture Guillaume Jacquenot · May 14, 2014

Basically there is no best format what will fit all your visualization needs.

It really depends on the visualizations you want to obtain.

For example, a Stacked Bar Chart takes as input a CSV file, and an adjacency matrix vizualisation takes a JSON format.

From my experience:

  • to display relations beetween items, like adjacency matrix or chord diagram, one will prefer a JSON format that will allow to describe only existing relations. Data are stored like in a sparse matrix, and several data can be nested using dictionary. Moreover this format can directly be parsed in Python.
  • to display properties of an array of items, a CSV format can be fine. A perfect example can be found here with a parallel chart display.
  • to display hierarchical data, like a tree, JSON is best suited.

The best thing to do to help you figure out what best format you need, is to have a look at this d3js gallery