1

I have a Seq and need to use it to select columns in Java

I know about the function .select(String col, Seq<String> cols) but I don't have the first column name.

2
  • 1
    df.columns returns an array with the names of the columns. Commented Jul 29, 2019 at 9:51
  • @philantrovert that does it, but why wouldn't they just keep a .select(Seq<String> cols) method Commented Jul 29, 2019 at 10:02

1 Answer 1

1

If you want to select using a Seq<String> you can split the Seq extracting the first element separately:

Seq<String> columns = /* ... */;

Dataframe<Row> newDf = df.select(
    columns.apply(0), // first element
    columns.slice(1, columns.size()) // from the second to the end
);

maybe check the length of columns first, to avoid IndexOutOfBoundsException

Sign up to request clarification or add additional context in comments.

1 Comment

select function takes in input (String,Seq<String>) but slice returns Scala iterable, so had to do toSeq() to it. columns.slice(...).toSeq()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.