Array of heterogeneous named tuples to DataFrame

Hi all-

I would like to convert an array of named tuples to a dataframe. This can be achieved with the DataFrame constructor when the named tuples have the same keys. However, in my case, the named tuples do not necessarily have the same keys. I would like to use missing in cases where one named tuple does not have a key that is in another named tuple. Here is a simple example:

np = [(x=1,y=2,z=3),(x=2,y=10)]

Desired Result:

 Row │ x      y      z       
     │ Int64  Int64  Int64?  
─────┼───────────────────────
   1 │     1      2        3
   2 │     2     10  missing 

Is there an easy way to do this? Thanks!

Push to an empty data frame in a loop

df = DataFrame()
for n in np
    push!(df, n; cols = :union)
end
3 Likes

I figured there was a better way as my attempts became increasingly convoluted. I totally forgot about the :union option. Thank you!