Good stuff. Here's a stab at the inverse:
function json2df(jsonstr)
dictvec = JSON.parse(jsonstr)
DataFrame(Dict(map(k->(k=>[row[k] for row in dictvec]), keys(dictvec[1
]
end
It doesn't pretty print, but is there anything wrong with this:
using JSON
json([[string(col)=> df[col][row] for col in names(df)] for row in
1:nrow(df)])
Just to break it down, this part creates the dictionary for each row:
[string(col)=> df[col][row] for col in names(df)]
and
Hi JobJob,
I like that too. Thanks!
When I got my df2json working, the next thing I naturally did was try
JSON.parse(json) and saw I got back an array of Dicts, so thought
constructing an array of Dicts from a DataFrame could also be a way to go,
but couldn't get it to work. Your solution
That is truly beautiful! Thank you Tomas
On Monday, November 9, 2015 at 5:01:10 PM UTC+8, Tomas Lycken wrote:
>
> I’d try to use the built-in tools for iterating and transforming stuff to
> make the code a little terser and easier to read:
>
> function df2json(df)
> io = IOBuffer()
>
I’d try to use the built-in tools for iterating and transforming stuff to
make the code a little terser and easier to read:
function df2json(df)
io = IOBuffer()
write(io, "[\n")
write(io, join(map(row -> "{\"A\": $(row[:A]), \"B\": \"$(row[:B])\"}",
eachrow(d)), ",\n"))
This is embarrassing since I'm just learning, but in the interest of
getting feedback and improving my Julia coding skills, here is what I did:
function df2json(df::DataFrame)
nrow,ncol = size(df)
io = IOBuffer();
write(io,"[\n")
for irow = 1:nrow
irow == nrow ? eor = "" : eor = ","