Skip to content

Instantly share code, notes, and snippets.

@skleinbo
Created August 20, 2018 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skleinbo/f187c7ef24e7614dc374645d1c1e5251 to your computer and use it in GitHub Desktop.
Save skleinbo/f187c7ef24e7614dc374645d1c1e5251 to your computer and use it in GitHub Desktop.
append two dataframes and pad with missing
using DataFrames
function append_padded(df1,df2)
result = DataFrame()
for col in names(df1)
new_col = nothing
if in(col, names(df2))
# vcat takes care of promoting to Union{Missing,T}
new_col = vcat(df1[col], df2[col])
else
new_col = vcat(df1[col], missings(size(df2,1)))
end
result[col] = new_col
end
for col in setdiff(names(df2),names(df1))
new_col = vcat(missings(size(df1,1)),df2[col])
result[col] = new_col
end
return result
end
df = DataFrame(a=[1,2,3], b=[1., 2., 3.])
ef = DataFrame(a=[4,5], c=["s1","s2"])
r = append_padded(df,ef)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment