javascript - How can I efficiently move from a Pandas dataframe to JSON -


i've started using pandas aggregation date. goal count of instances of measurement occur on particular day, , represent in d3. illustrate workflow, have queryset (from django) looks this:

queryset = [{'created':"05-16-13", 'counter':1, 'id':13}, {'created':"05-16-13", 'counter':1, 'id':34}, {'created':"05-17-13", 'counter':1, 'id':12}, {'created':"05-16-13", 'counter':1, 'id':7}, {'created':"05-18-13", 'counter':1, 'id':6}] 

i make dataframe in pandas , aggregate measure 'counter' day created:

import pandas pd queryset_df = pd.dataframe.from_records(queryset).set_index('id') aggregated_df = queryset_df.groupby('created').sum() 

this gives me dataframe this:

          counter created           05-16-13        3 05-17-13        1 05-18-13        1 

as i'm using d3 thought json object useful. using pandas to_json() function convert dataframe this:

aggregated_df.to_json() 

giving me following json object

{"counter":{"05-16-13":3,"05-17-13":1,"05-18-13":1}} 

this not want, able access both date, , measurement. there way can export data such end this?

data = {"c1":{"date":"05-16-13", "counter":3},"c2":{"date":"05-17-13", "counter":1}, "c3":{"date":"05-18-13", "counter":1}} 

i thought if structure differently on python side, reduce amount of data formatting need on js side planned load data doing this:

  x.domain(d3.extent(data, function(d) { return d.date; }));   y.domain(d3.extent(data, function(d) { return d.counter; })); 

i'm open suggestions of better workflows overall need unsure of best way of handling connection between d3 , pandas. (i have looked @ several packages combine both python , d3 directly, not looking seem focus on static chart generation , not making svg)

transform date index simple data column reset_index, , generate json object using orient='index' property:

in [11]: aggregated_df.reset_index().to_json(orient='index') out[11]: '{"0":{"created":"05-16-13","counter":3},"1":{"created":"05-17-13","counter":1},"2":{"created":"05-18-13","counter":1}}' 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -