How to insert a column in a julia DataFrame at specific position (without referring to existing column names)
Update: This function has changed. See @DiegoJavierZea ’s comment.
Well, congratulate you found a workaround your self, but there is a built-in function that is semantically more clear and possibly a little bit faster:
using DataFrames
df = DataFrame(
colour = ["green","blue"],
shape = ["circle", "triangle"],
border = ["dotted", "line"]
)
insert!(df, 3, [1,2], :area)
Where 3
is the expected index for the new column after the insertion, [1,2]
is its content, and :area
is the name. You can find a more detailed document by typing ?insert!
in REPL after loading the DataFrames
package.
It is worth noting that the !
is a part of the function name. It's a Julia convention to indicate that the function will mutate its argument.