Created
August 20, 2018 16:08
-
-
Save skleinbo/f187c7ef24e7614dc374645d1c1e5251 to your computer and use it in GitHub Desktop.
append two dataframes and pad with missing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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