Stream: helpdesk (published)

Topic: Joining DataFrames in the row number


view this post on Zulip Davi Sales Barreira (Jan 13 2022 at 14:50):

Is there an easy way to join dataframes according to the row number? I have one dataframe like:

| c1 | c2 |
  a     -
  b     -

and another with:

| c1 | c2 | c3 |
   -    d   v
   -    a   w

And I'd like to produce:

| c1 | c2 | c3 |
   a   d    v
   b   a    w

Any thoughts?

view this post on Zulip Sundar R (Jan 13 2022 at 15:07):

To just join them together by row number, you can use hcat:

julia> df1 = DataFrame(c1 = 1:5);
julia> df2 = DataFrame(c2 = 9:13, c3=[1,2,3,5,8]);
julia> hcat(df1, df2)
5×3 DataFrame
 Row │ c1     c2     c3
     │ Int64  Int64  Int64
─────┼─────────────────────
   1 │     1      9      1
   2 │     2     10      2
   3 │     3     11      3
   4 │     4     12      5
   5 │     5     13      8

view this post on Zulip Sundar R (Jan 13 2022 at 15:11):

In your case, you might have to drop your blank columns (are the - representing missing values?) with Not indexing: hcat(df1[!, Not(:c2)], df2[!, Not(:c1)])

view this post on Zulip Davi Sales Barreira (Jan 13 2022 at 15:13):

Thanks, @Sundar R


Last updated: Nov 22 2024 at 04:41 UTC