Skip to content
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

Support Vector{Dict} as a Table format #132

Closed
kmsquire opened this issue Feb 3, 2020 · 2 comments
Closed

Support Vector{Dict} as a Table format #132

kmsquire opened this issue Feb 3, 2020 · 2 comments

Comments

@kmsquire
Copy link

kmsquire commented Feb 3, 2020

(Prompted by @quinnj on Slack)

Occasionally, I end up with a Vector{Dict} that I would like to turn into a DataFrame (or some other table). Right now, there is no direct path (that I could find).

@r-barnes
Copy link

r-barnes commented Feb 4, 2020

New Julia user here trying to build a work around:

using DataFrames

function DictionariesToDataFrame(dictlist)
  ret = Dict()                 #Holds dataframe's columns while we build it
  #Get all unique keys from dictlist and make them entries in ret
  for x in unique([y for x in [collect(keys(x)) for x in dictlist] for y in x])
    ret[x] = []
  end
  for row in dictlist          #Loop through each row
    for (key,value) in ret     #Use ret to check all possible keys in row
      if haskey(row,key)       #Is key present in row?
        push!(value, row[key]) #Yes
      else                     #Nope
        push!(value, nothing)  #So add nothing. Keeps columns same length.
      end
    end
  end
  #Fix the data types of the columns
  for (k,v) in ret                             #Consider each column
    row_type = unique([typeof(x) for x in v])  #Get datatypes of each row
    if length(row_type)==1                     #All rows had same datatype
      row_type = row_type[1]                   #Fetch datatype
      ret[k]   = convert(Array{row_type,1}, v) #Convert column to that type
    end
  end
  #DataFrame is ready to go!
  return DataFrames.DataFrame(ret)
end

#Generate some data
dictlist=[]
for i in 1:20
  push!(dictlist, Dict("a"=>i, "b"=>2*i))
  if i>10
    dictlist[end-1]["c"]=3*i
  end
end

DictionariesToDataFrame(dictlist)

Yaaaaay :-(

@quinnj
Copy link
Member

quinnj commented Feb 8, 2020

Implemented in #131

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants