I would like to row bind two dataframes with different columns like bind_rows
in R. I tried the same with vcat
as in this question: Julia equivalent of dplyr's bind_cols and bind_rows, but I get an error. Here some reproducible code:
using DataFrames
df1 = DataFrame(a = 1, b = 1)
df2 = DataFrame(b = 1, c = 1)
vcat(df1, df2)
Output:
ArgumentError: column(s) c are missing from argument(s) 1, and column(s) a are missing from argument(s) 2
I don't know why this error happens, because in the answer of the question it does work. So I was wondering if anyone knows why this error happens and if this is the best way to bind two dataframes in Julia?
CodePudding user response:
The error occurs because those DataFrames have columns with different names (technically, properties). Use the cols=:union
keyword argument:
vcat(df1, df2, cols=:union)
# 2×3 DataFrame
# Row │ a b c
# │ Int64? Int64 Int64?
# ─────┼─────────────────────────
# 1 │ 1 1 missing
# 2 │ missing 1 1
union
holds a union of columns. cols
have more options. check them out here if you are interested.