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)
Comment From: jreback
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)]
Comment From: jreback
cc @Komnomnomnom
related is #5729
Comment From: im-n1
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.
Comment From: jreback
well @grafa pull-requests are welcome.
Comment From: im-n1
I would like to help but I'm not such experienced to deliver solution that matches Pandas code quality.
Comment From: jreback
closing as dupe of #5729