Wanted to double-check if there are any recommended ways to convert a DataFrame (or DataFrameRow) into a Dict (for eventually conversion to JSON). This is the inverse of this question.
“List of Dicts” JSON format would be ideal.
For example, converting:
df = DataFrame(x = 1:3, y = 4:6)
3×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 1 │ 4 │
│ 2 │ 2 │ 5 │
│ 3 │ 3 │ 6 │
to
[{"x":1,"y":4}
,{"x":2,"y":5}
,{"x":3,"y":6}
]
Here’s my basic implementation. Was wondering if there’s anything more efficient out there.
function dataframe_to_json(df)
li = []
for row in eachrow(df)
di = Dict()
for name in names(row)
di[string(name)] = row[name]
end
push!(li, di)
end
JSON.json(li)
end
df = DataFrame(x = 1:3, y = 4:6)
dataframe_to_json(df)