How to move my pandas dataframe to d3?
Asked Answered
P

2

13

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.

Phalange answered 14/5, 2014 at 15:39 Comment(2)
There's df.to_json or df.to_csv for moving the data around. There's vincent for a python -> vega translator (it supports DataFrames well). And another project of Rob's, sticky, is in alpha, but it sounds like what you want for not leaving the IPython notebook.Tyrus
I know you said you dislike matplotlib, but have you looked at mpld3? It's quite nice, I've had a recent success using it to make an online interactive plot.Halimeda
A
5

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

Antenna answered 14/5, 2014 at 16:43 Comment(1)
So basically the takeaway is that there is no "best" way to move data between them?Phalange
S
1

You can use D3 directly inside of Jupyter / Ipython. Try the two links below ..

http://blog.thedataincubator.com/2015/08/embedding-d3-in-an-ipython-notebook/

https://github.com/cmoscardi/embedded_d3_example/blob/master/Embedded_D3.ipynb

Storm answered 4/1, 2017 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.