-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_process.py
More file actions
38 lines (26 loc) · 1.18 KB
/
data_process.py
File metadata and controls
38 lines (26 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import json
def formatForD3( pandas_json ):
""" Re-format JSON regression results to be more friendly to D3.js,
the plotting library used by the web front-end.
JSON output from pandas is 'dict-like', that is regression coefficients and
values are stored as key-val pairs in a dictionary.
There is no particulary convenient way to handle this type of data using
D3.js, which works best with an array of two-element dictionaries where
one key in the dictionary is the element 'Name' and the other key
is the element 'Value'.
As an example this function would take the pandas formatted JSON object
{0: {Intercept: 20.8178307102, rm: 4.2452889618, lstat: -0.5213794328}}
and return
[{Name: "lstat", Val: -0.5213794328}, {Name: "Intercept", Val: 20.8178307102},{Name: "lstat", Val: -0.5213794328}]
Args:
pandas_json: JSON object output from pandas.DataFrame.to_json(orient='columns')
Returns:
Reformatted JSON object.
"""
json_data = json.loads( pandas_json )['0']
li = []
for x in json_data:
key = x
val = json_data[x]
li.append( { 'Name':x, 'Val':val } )
return ( json.dumps( li ) )