Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join on different columns

I want to join 2 dataframes and I'm using the intructions in the Julia guide: http://dataframesjl.readthedocs.org/en/latest/joins_and_indexing.html?highlight=join

join(a, b, on = :ID, kind = :inner)

Now, how I can join on columns with different names:

  • :ID for a
  • :name_id for b

I tried the following but it doesn't work"

join(a, b, on = [ :ID, :name_id ], kind = :inner)

If not implemented, that would be nice feature

like image 481
Guillaume Avatar asked Dec 21 '14 21:12

Guillaume


2 Answers

rename!(b, "name_id", "ID")
j = join(a, b, on = :ID, kind = :inner)
rename!(b, "ID", name_id")

The rename changes b, so here I changed it back. (In the general case if there are renaming conflicts within a dataframe you can pick a unique name for each pair of joined names, rename in each dataframe, join and rename back.)

This method is obviously not elegant. It's what I've found so far.

like image 114
philipxy Avatar answered Sep 28 '22 10:09

philipxy


The docs on DataFrames.jl provide this example:

innerjoin(a, b, on = [:City => :Location, :Job => :Work])

So for your example:

j = innerjoin(a, b, on = :ID => :name_id)
like image 41
FeFiFoFu Avatar answered Sep 28 '22 10:09

FeFiFoFu