How to select rows of a dataframe?


I have a dataframe df with 5 rows and a vector of vectors v=[[3],[5],[1,2],[4]].
I want to create a new dataframe dg with rows indexes sorted in order of v:

  • the 1st row of dg is the 3rd row of df
  • the 2nd row of dg is the 5th row of df
  • the 3rd row of dg is the 1st row of df
  • the 4th row of dg is the 2nd row of df
  • the 5th row of dg is the 4th row of df

Thanks for your help.

Easiest way is probably to use vcat and splatting:

df = DataFrame(x=1:5)
v = [[3],[5],[1,2],[4]]
df[vcat(v...), :]

 Row │ x
     │ Int64
   1 │     3
   2 │     5
   3 │     1
   4 │     2
   5 │     4

Are you sure these indices in the vector couldn’t be replaced by the usual split-apply-combine workflow with dataframes? Maybe these indices are being computed from other columns of the original dataframe?

It works !