holgerbrandl / krangl

krangl is a {K}otlin DSL for data w{rangl}ing
MIT License
561 stars 50 forks source link

AddColumn At Index #112

Closed bkenn closed 3 years ago

bkenn commented 3 years ago

Hello!

There are times where I want a column or a list of columns added at a specific index. For my use case I would like to add multiple columns to the beginning of an existing data frame. Currently, I perform the following, but this requires moveLeft.

DataFrame.readCSV(file)
    .addColumns(
        ColumnFormula("side") { "right" },
        ColumnFormula("type") { "-+" }
    )
   .moveLeft("side", "type")

I would like some way in Column Formula to define the index of insertion and move the other columns over to the right. Or maybe just a helper method like fun addLeft(vararg columnFormulas: ColumnFormula). I'll see if I can make a POC.

holgerbrandl commented 3 years ago

See http://holgerbrandl.github.io/krangl/faq/#how-to-add-a-column-at-a-certain-index-position for one way to implement it.

Since I'd consider it rather a rare use-case I'm unsure if it should be part of the API (which is complex enough imho)