Skip to content

to_json() Flat Output #8333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
r-barnes opened this issue Sep 20, 2014 · 6 comments
Closed

to_json() Flat Output #8333

r-barnes opened this issue Sep 20, 2014 · 6 comments
Labels
Duplicate Report Duplicate issue or pull request Enhancement IO JSON read_json, to_json, json_normalize

Comments

@r-barnes
Copy link

The to_json() I/O command current provides the following output:

split   dict like {index -> [index], columns -> [columns], data -> [values]}
records list like [{column -> value}, ... , {column -> value}]
index   dict like {index -> {column -> value}}
columns dict like {column -> {index -> value}}
values  just the values array

It would be handy if the output could also be provided in the format:

mdarray    [ [index1,value1a,value1b,...] , [index2,value2a,value2b,...], ... ]

This format is, for instance, used by the DyGraphs JS library for plotting data.

The following command provides the output I am looking for:

np.column_stack((df.index,df.as_matrix())).tolist()

(where df is a DataFrame)

@jreback
Copy link
Contributor

jreback commented Sep 20, 2014

Not nearly as efficient (as json is implemented in c), but you can do this (and its a python structure not JSON....but)

In [14]: df = DataFrame(np.arange(10).reshape(5,-1),columns=list('AB'))

In [15]: df
Out[15]: 
   A  B
0  0  1
1  2  3
2  4  5
3  6  7
4  8  9

In [16]: list(df.itertuples())
Out[16]: [(0, 0, 1), (1, 2, 3), (2, 4, 5), (3, 6, 7), (4, 8, 9)]

@jreback jreback added Enhancement IO JSON read_json, to_json, json_normalize labels Sep 20, 2014
@jreback
Copy link
Contributor

jreback commented Sep 20, 2014

cc @Komnomnomnom

related is #5729

@jreback jreback added this to the 0.15.1 milestone Sep 20, 2014
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 6, 2015
@im-n1
Copy link

im-n1 commented Oct 8, 2016

Hi there,

this feature is really needed. These flat arrays [val, val, val, ...] are used by Javascript libraries.

All the solutions above don't work so my way was df.to_csv() where I get the flat rows and then I'm reading this csv. It's nasty but I had no choice.

@jreback
Copy link
Contributor

jreback commented Oct 8, 2016

well @grafa pull-requests are welcome.

@im-n1
Copy link

im-n1 commented Oct 8, 2016

I would like to help but I'm not such experienced to deliver solution that matches Pandas code quality.

@jreback
Copy link
Contributor

jreback commented Sep 18, 2017

closing as dupe of #5729

@jreback jreback closed this as completed Sep 18, 2017
@jorisvandenbossche jorisvandenbossche modified the milestones: Next Major Release, No action Sep 18, 2017
@jorisvandenbossche jorisvandenbossche added the Duplicate Report Duplicate issue or pull request label Sep 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request Enhancement IO JSON read_json, to_json, json_normalize
Projects
None yet
Development

No branches or pull requests

4 participants