简体   繁体   中英

Assignment within Spark Scala foreach Loop

I'm new to scala/spark and am trying to loop through a dataframe and assign the results as the loop progresses. The following code works but can only print the results to screen.

traincategory.columns.foreach { x=>

val test1 = traincategory.select("Id", x)

import org.apache.spark.ml.feature.{OneHotEncoder, StringIndexer}

//CODE TO PERFORM ONEHOT TRANSFORMATION

val encoded = encoder.transform(indexed)

encoded.show()

}

As val is immutable I have attempted to append the vectors from this transformation onto another variable, as might be done in R.

//var ended = traincategory.withColumn(x,encoded(0))

I suspect Scala has a more idiomatic way of processing this.

Thank you in advance for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM